C#?DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹狀模型數(shù)據(jù)
關(guān)于 FlyTreeView
NineRays.WebControls.FlyTreeView 是 9rays.net 推出的一款功能強(qiáng)大的樹狀模型數(shù)據(jù)顯示控件,本文將介紹使用其 Asp.net 版本控件,并結(jié)合 DataSet 對(duì)象進(jìn)行數(shù)據(jù)顯示。
顯示效果如下圖:
DataSet 數(shù)據(jù)準(zhǔn)備
我們?cè)?MS SQL Server 創(chuàng)建 groupUsers(群組用戶表),其結(jié)構(gòu)如下表:
序號(hào) | 字段名 | 類型 | 說明 |
---|---|---|---|
1 | cid | uniqueidentifier | 唯一標(biāo)識(shí) |
2 | Group_Cid | uniqueidentifier | 所屬群組ID標(biāo)識(shí)(引用群組表groups) |
3 | Account_Cid | uniqueidentifier | 人員帳戶ID(引用用戶表Accounts,用于顯示昵稱、姓名等) |
4 | parent_Cid | uniqueidentifier | 父結(jié)點(diǎn)ID,所屬管理者ID |
5 | sortcode | int | 同級(jí)排序號(hào) |
6 | sys_insuser | nvarchar(100) | 創(chuàng)建者用戶名 |
7 | sys_instime | datetime | 創(chuàng)建時(shí)間 |
8 | sys_upduser | nvarchar(100) | 最后修改者用戶名 |
9 | sys_updtime | datetime | 最后修改時(shí)間 |
該表所涉及的引用表這里不在贅述,我們假設(shè)有如下 SQL 語句:
select a.cid,a.parent_cid,nickname+'('+name+')' truename from groupUsers a,accounts b where a.group_cid=@group_cid and a.account_cid=b.cid
我們需要重點(diǎn)得到 cid(唯一標(biāo)識(shí))、parent_cid(父節(jié)點(diǎn)ID) 和 truename (顯示名稱) 三個(gè)字段。查詢并寫入到 DataSet 中。
涉及表結(jié)構(gòu)創(chuàng)建腳本
群組用戶表
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[groupUsers]( [cid] [uniqueidentifier] ROWGUIDCOL NOT NULL, [Group_Cid] [uniqueidentifier] NOT NULL, [Account_Cid] [uniqueidentifier] NOT NULL, [parent_Cid] [uniqueidentifier] NULL, [sortcode] [int] NULL, [sys_insuser] [nvarchar](100) NULL, [sys_instime] [datetime] NULL, [sys_upduser] [nvarchar](100) NULL, [sys_updtime] [datetime] NULL, CONSTRAINT [PK_groupUsers] PRIMARY KEY CLUSTERED ( [cid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [IX_cc_groupUsers] UNIQUE NONCLUSTERED ( [Group_Cid] ASC, [parent_Cid] ASC, [Account_Cid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[groupUsers] ADD CONSTRAINT [DF_groupUsers_cid] DEFAULT (newid()) FOR [cid] GO
用戶表
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[accounts]( [cid] [uniqueidentifier] ROWGUIDCOL NOT NULL, [name] [nvarchar](50) NULL, [nickname] [nvarchar](500) NULL )GO ALTER TABLE [dbo].[accounts] ADD CONSTRAINT [DF_accounts_cid] DEFAULT (newid()) FOR [cid] GO
范例運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
數(shù)據(jù)庫:Microsoft SQL Server 2016
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
方法設(shè)計(jì)
simpletreeview方法返回結(jié)點(diǎn)總數(shù),其參數(shù)說明見下表:
序號(hào) | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | tv | FlyTreeNodeCollection | 傳入的FlyTreeView的當(dāng)前結(jié)點(diǎn)集合對(duì)象 |
2 | ds | DataSet | 數(shù)據(jù)集對(duì)象,默認(rèn)只取Tables[0] |
3 | key | string | 數(shù)據(jù)表的唯一標(biāo)識(shí)字段名 |
4 | parentkey | string | 數(shù)據(jù)表的父結(jié)點(diǎn)字段名 |
5 | dis | string | 數(shù)據(jù)表的顯示名稱字段名 |
6 | keytype | string | 標(biāo)識(shí)類型,這是我們自定的規(guī)范,比如CID(字符)、ID(數(shù)值)固定名稱的處理方式,默認(rèn)處理方式對(duì)key或parentKey進(jìn)行字符串過濾處理 |
7 | initvalue | string | 是否指定一個(gè)初始值 |
8 | firstlevel | bool | 是否指遍歷一級(jí),如果為true,則不在進(jìn)行遞歸 |
9 | initByKey | bool | 初始值使用哪個(gè)關(guān)鍵字段,false使用父節(jié)點(diǎn),true使用唯一標(biāo)識(shí),默認(rèn)為false |
代碼實(shí)現(xiàn)
方法代碼
int simpletreeview(fwebcontrols.FlyTreeNodeCollection tv, DataSet ds, string key, string parentkey, string dis, string keytype, string initvalue, bool firstlevel,bool initByKey) { int rv = 0; DataView dv = new DataView(); dv.Table = ds.Tables[0]; fwebcontrols.FlyTreeNode tmpNd; switch (keytype) { case "cid": dv.RowFilter = initvalue == "" ? " " + (initByKey == false ? parentkey : key) + " is null " : " " + (initByKey == false ? parentkey : key) + "='" + initvalue + "'"; break; case "id": dv.RowFilter = initvalue == "" ? " " + (initByKey == false ? parentkey : key) + " is null " : "" + (initByKey == false ? parentkey : key) + "=" + initvalue + ""; break; default: dv.RowFilter = "isnull(" + (initByKey == false ? parentkey : key) + ",'')='" + initvalue + "'"; break; } rv = dv.Count; foreach (DataRowView drv in dv) { tmpNd = new fwebcontrols.FlyTreeNode(); tmpNd.Text = drv[dis].ToString(); tmpNd.Value = drv[key].ToString(); tv.Add(tmpNd); if (!firstlevel) simpletreeview(tmpNd.ChildNodes, ds, key, parentkey, dis, keytype, tmpNd.Value,firstlevel,false); } return rv; }
調(diào)用示例
我們首先需要在頁面注冊(cè)控件,代碼如下:
<%@ Register Assembly="Microsoft.Web.UI.WebControls" Namespace="Microsoft.Web.UI.WebControls" TagPrefix="codn" %>
前端代碼如下:
<NineRays:FlyTreeView ID="userTree" ExpandLevel="4" Width="100%" CssClass="form-control" runat="server" Style="padding-top: 10px; padding-bottom: 10px; height: 400px;" CanBeSelected="true" BackColor="White" PostBackOnSelect="False" EnableTheming="True" ImageSet="Office2003" ImageSetCustomPath="/images/vista/" ContentClickTogglesCheckbox="True" IsCheckbox="True"> <SelectedStyle BackColor="#BFCFFF" /> </NineRays:FlyTreeView>
后端調(diào)用代碼如下:
userTree.Nodes.Clear(); object ds=InitDataSet(); string initvalue=""; if(RowsCount==0) return 0; simpletreeview(userTree.Nodes, (DataSet)ds, "cid", "parent_cid", "truename", "cid","", false, initvalue != "" ? true : false); userTree.ContentClickCollapses = true; userTree.ContentClickExpands = true;
小結(jié)
1、示例代碼中如何獲取 DataSet 數(shù)據(jù)需要我們自行進(jìn)行編寫代碼處理,這里只是抽象展示。
2、在 VS 中開發(fā)我們需要在 IDE環(huán)境解決方案中添加此 dll,并引用,如下代碼:
3、提供一個(gè)后端輔助方法 getFlyTreeViewAllNodes,可以獲得FlyTreeView的所有結(jié)點(diǎn)信息,并放置到 ArrayList 中。
其參數(shù)說明見下表:
序號(hào) | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | tv | FlyTreeNodeCollection | 要遍歷的TreeView集合 |
2 | rv2 | ArrayList | 要存儲(chǔ)的 ArrayList 變量 |
方法代碼如下:
public void getFlyTreeViewAllNodes(fwebcontrols.FlyTreeNodeCollection tv, ArrayList rv2) { for (int i = 0; i < tv.Count; i++) { rv2.Add(tv[i].Value); getFlyTreeViewAllNodes(tv[i].ChildNodes, rv2); } }
方法會(huì)在指定的 ArrayList 里存儲(chǔ) TreeView 的 Value 值 。
關(guān)于 NineRays.WebControls.FlyTreeView 更多操作這里不再做進(jìn)一步說明,請(qǐng)參考其官網(wǎng)說明。
到此這篇關(guān)于C# DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹狀模型數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C# DataSet顯示樹狀模型數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器的示例詳解
以往一般都是用 System.Timers.Timer 來做計(jì)時(shí)器,其實(shí) System.Threading.Timer 也可以實(shí)現(xiàn)計(jì)時(shí)器功能,下面就跟隨小編一起來學(xué)習(xí)一下如何使用System.Threading.Timer實(shí)現(xiàn)計(jì)時(shí)器功能吧2024-01-01利用C#實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換功能
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12WPF實(shí)現(xiàn)基礎(chǔ)控件之托盤的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)托盤這一基礎(chǔ)控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-10-10c# in depth的泛型實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了c# in depth的泛型實(shí)現(xiàn)實(shí)例代碼,學(xué)C#的同學(xué)一定會(huì)用到泛型實(shí)現(xiàn)的,這里我們提供了泛型實(shí)現(xiàn)的程序代碼,大家參考使用2013-11-11如何使用C#修改本地Windows系統(tǒng)時(shí)間
這篇文章主要介紹了如何使用C#修改本地Windows系統(tǒng)時(shí)間,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#中sqlDataRead 的三種方式遍歷讀取各個(gè)字段數(shù)值的方法
這篇文章主要介紹了C#中 sqlDataRead 的三種方式遍歷讀取各個(gè)字段數(shù)值的方法,每種方法給大家介紹的都非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
這篇文章主要介紹了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法,結(jié)合具體實(shí)例形式分析了數(shù)據(jù)庫的創(chuàng)建及checkedListBox1控件連接數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06