递归方式实现树的展示形式

1.模拟出数据

 1 private DataTable CreateDataTable()
 2         {
 3             DataTable table = new DataTable();
 4             DataColumn column1 = new DataColumn("Id", typeof(string));
 5             DataColumn column2 = new DataColumn("Text", typeof(String));
 6             DataColumn column3 = new DataColumn("ParentId", typeof(string));
 7             table.Columns.Add(column1);
 8             table.Columns.Add(column2);
 9             table.Columns.Add(column3);
10 
11             DataRow row = table.NewRow();
12             row[0] = "china";
13             row[1] = "中国";
14             row[2] = DBNull.Value;
15             table.Rows.Add(row);
16 
17             row = table.NewRow();
18             row[0] = "henan";
19             row[1] = "河南省";
20             row[2] = "china";
21             table.Rows.Add(row);
22 
23             row = table.NewRow();
24             row[0] = "zhumadian";
25             row[1] = "驻马店市";
26             row[2] = "henan";
27             table.Rows.Add(row);
28 
29             row = table.NewRow();
30             row[0] = "luohe";
31             row[1] = "漯河市";
32             row[2] = "henan";
33             table.Rows.Add(row);
34 
35             row = table.NewRow();
36             row[0] = "anhui";
37             row[1] = "安徽省";
38             row[2] = "china";
39             table.Rows.Add(row);
40 
41             row = table.NewRow();
42             row[0] = "hefei";
43             row[1] = "合肥市";
44             row[2] = "anhui";
45             table.Rows.Add(row);
46 
47             row = table.NewRow();
48             row[0] = "golden";
49             row[1] = "金色池塘小区";
50             row[2] = "hefei";
51             table.Rows.Add(row);
52 
53             row = table.NewRow();
54             row[0] = "ustc";
55             row[1] = "中国科学技术大学";
56             row[2] = "hefei";
57             table.Rows.Add(row);
58 
59             return table;
60         }
View Code

2.添加树的数据

 1 private void LoadData()
 2         {
 3             // 模拟从数据库返回数据表
 4             DataTable table = CreateDataTable();
 5             DataSet ds = new DataSet();
 6             ds.Tables.Add(table);
 7             ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["Id"], ds.Tables[0].Columns["ParentId"]);
 8 
 9             foreach (DataRow row in ds.Tables[0].Rows)
10             {
11                 if (row.IsNull("ParentId"))
12                 {
13                     TreeNode node = new TreeNode();
14                     node.Text = row["Text"].ToString();
15                     node.Expanded = true;
16                     tree.Nodes.Add(node);
17                     ResolveSubTree(row, node);
18                 }
19             }
20         }
View Code

3.递归实现无限极添加

 1 private void ResolveSubTree(DataRow dataRow, TreeNode treeNode)
 2         {
 3             DataRow[] rows = dataRow.GetChildRows("TreeRelation");
 4             if (rows.Length > 0)
 5             {
 6                 treeNode.Expanded = true;
 7                 foreach (DataRow row in rows)
 8                 {
 9                     TreeNode node = new TreeNode();
10                     node.Text = row["Text"].ToString();
11                     treeNode.ChildNodes.Add(node);
12                     ResolveSubTree(row, node);
13                 }
14             }
15         }
View Code

 

 

posted @ 2014-06-26 22:18  Suner  阅读(303)  评论(0编辑  收藏  举报