本文实例讲述了C# TreeView无限目录树实现方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#region 绑定客户树 protected void bindTreeView() { TreeView1.Nodes.Clear(); string userid = Session[ "UserID" ].ToString(); string sqlwr = new SY_ADMINUSER().GetUserIDListByLoginUser(userid, "CUSTOMERSERVICE" , false ); DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query( "Select PARENTID,CUSTOMERID,CUSTOMERSHORT,CUSTOMERSERVICE from CU_CUSTOMER where ISDELETE='0' " + sqlwr + " order by CUSTOMERSHORT " ).Tables[0]; //定义临时树节点 TreeNode tmpNd; //特殊处理 foreach (DataRow dr in dt.Rows) { DataRow[] drs = dt.Select( "CUSTOMERID='" + dr[ "PARENTID" ] + "'" ); if (drs.Length == 0) //无上级节点 { dr[ "PARENTID" ] = "0" ; } } dt.AcceptChanges(); //从DataTable中得到所有父节点为null的DataRow形成的数组 DataRow[] rows = dt.Select( "PARENTID='0'" ); //遍历根节点数组 foreach (DataRow row in rows) { tmpNd = new TreeNode(); //给根节点赋值 tmpNd.Value = row[ "CUSTOMERID" ].ToString(); tmpNd.Text = row[ "CUSTOMERSHORT" ].ToString(); tmpNd.Expanded = true ; //将节点加入到树中 TreeView1.Nodes.Add(tmpNd); AddTree(dt, TreeView1, tmpNd, row[ "CUSTOMERID" ].ToString()); } } public void AddTree(DataTable dt, TreeView tv, TreeNode pNode, string CUSTOMERID) { DataRow[] rows = dt.Select( "PARENTID='" + CUSTOMERID + "'" ); TreeNode tmpNd; foreach (DataRow row in rows) { tmpNd = new TreeNode(); //给根节点赋值 tmpNd.Value = row[ "CUSTOMERID" ].ToString(); tmpNd.Text = row[ "CUSTOMERSHORT" ].ToString(); tmpNd.Expanded = true ; pNode.ChildNodes.Add(tmpNd); //将节点加入到树中 //TreeView1.Nodes.Add(pNode); AddTree(dt, TreeView1, tmpNd, row[ "CUSTOMERID" ].ToString()); } } #endregion |
希望本文所述对大家C#程序设计有所帮助。