递归查询区域信息及子区域到advTree

效果:

 1  DataTable dtArea = new DataTable();
 2 
 3 private void Form1_Load(object sender, EventArgs e)
 4 {
 5 Node node1 = new Node();
 6 node1.Name = "0";
 7 node1.Text = "所有区域";
 8 node1.DataKey = null;
 9 node1.TagString = "0";
10 advTree1.Nodes.Add(node1);
11 
12 dtArea = new DataTable();
13 BaseServer.BaseInforClient bll = new BaseServer.BaseInforClient();
14 
15 Dt_areasInfo model = new Dt_areasInfo() { Category = EnumMakeNumber.dt_areas };
16 BaseDtoOfDataSetymujubN2 dto = bll.QueryData(BaseServer.ExternalStructEnumOperateObj.查询, model);
17 
18 if (dto.Code == 200)
19 {
20 dtArea = dto.Data.Tables[0];
21 BindTree(dtArea, node1);
22 }
23 //展开
24 advTree1.ExpandAll();
25 //关闭
26 //advTree1.CollapseAll();
27 }
28 public void BindTree(DataTable dt, Node node)
29 {
30 DataView dv = dt.DefaultView;
31 dv.RowFilter = " pid=" + node.Name;
32 DataTable dtnew = dv.ToTable();
33 if (dtnew.Rows.Count > 0)
34 foreach (DataRow item in dtnew.Rows)
35 {
36 Node nodec = new Node();
37 nodec.Name = item["id"].ToString();
38 nodec.Text = "(" + item["id"].ToString() + ")" + item["name"].ToString();
39 nodec.DataKey = item;
40 nodec.TagString = item["sort"].ToString();
41 node.Nodes.Add(nodec);
42 BindTree(dt, nodec);
43 }
44 }
45 
46 private void advTree1_AfterSelect(object sender, EventArgs e)
47 {
48 if (advTree1.SelectedNode != null)
49 {
50 MessageBox.Show(advTree1.SelectedNode.Text);
51 }
52 }
View Code

转载请注明出入来源:https://www.cnblogs.com/famhuai/p/8252922.html

posted @ 2018-01-09 18:27  繁华初上  阅读(318)  评论(0编辑  收藏  举报