C#递归树

 1 protected void Page_Load(object sender, EventArgs e) 
 2 { 
 3 bindtree(PopId); 
 4 } 
 5 private void bindtree() 
 6 { 
 7 TreeView1.Nodes.Clear(); 
 8 AddTree(0, (TreeNode)null); 
 9 } 
10 public void AddTree(int ParentID, TreeNode pNode) 
11 { 
12 DataTable dt = new DataTable(); 
13 //数据源 
14 dt = logbll.BindPop(); 
15 DataView dvTree = new DataView(dt); 
16 //过滤ParentID,得到当前的所有子节点 
17 dvTree.RowFilter = "flid = " + ParentID; 
18 
19 foreach (DataRowView Row in dvTree) 
20 { 
21 TreeNode Node = new TreeNode(); 
22 if (pNode == null) 
23 { //添加根节点 
24 Node.Text = Row["PopName"].ToString(); 
25 TreeView1.Nodes.Add(Node); 
26 //Node.Expanded = true; 
27 
28 AddTree(Int32.Parse(Row["PopId"].ToString()), Node); //再次递归 
29 } 
30 else 
31 { //添加当前节点的子节点 
32 Node.Text = Row["PopName"].ToString(); 
33 Node.NavigateUrl = Row["PostUrl"].ToString(); 
34 pNode.ChildNodes.Add(Node); 
35 //Node.Expanded = true; 
36 AddTree(Int32.Parse(Row["PopId"].ToString()), Node); //再次递归 
37 } 
38 } 
39 }

 

C#递归树

上一篇:2021-07-28 MVN打包项目及将源码加入本地仓库


下一篇:MVC4 WebAPI(一)