![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 递归绑定: DataSet ds = new DataSet();
2 ds = c.TreeView(Sql);
3 DataView dr1 = new DataView();
4 dr1.Table = ds.Tables[0];
5 foreach (DataRowView drv1 in dr1)
6 {
7 TreeNode tn1 = new TreeNode();
8 获取编号
9 tn1.Tag = drv1["RP_Code"].ToString();
10 获取名称
11 tn1.Text = drv1["RP_Name"].ToString();
12 添加根级目录
13 treeView1.Nodes.Add(tn1);
14 tn1.Expand();
15 //查找编号长度为3选项——二级
16 string SqlP = "select * from YXL_TreeViewInfo where LEN(RP_Code)=3 ";
17 ds = c.TreeView(SqlP);
18 DataView dr2 = new DataView();
19 dr2.Table = ds.Tables[0];
20 foreach (DataRowView drv2 in dr2)
21 {
22 TreeNode tn2 = new TreeNode();
23 tn2.Tag = drv2["RP_Code"].ToString();
24 tn2.Text = drv2["RP_Name"].ToString();
25 //图片
26 tn2.ImageIndex = 1;
27 tn1.Nodes.Add(tn2);
28
29 //查找编号长度为3选项——三级
30 string SqlC = "select * from YXL_TreeViewInfo where RP_Code like '" + tn2.Tag + "%' and LEN(RP_Code)=6";
31 ds = c.TreeView(SqlC);
32 DataView dv3 = new DataView();
33 dv3.Table = ds.Tables[0];
34 foreach (DataRowView drv3 in dv3)
35 {
36 TreeNode tn3 = new TreeNode();
37 tn3.Tag = drv3["RP_Code"].ToString();
38 tn3.Text = drv3["RP_Name"].ToString();
39 tn3.ToolTipText = drv3["TableName"].ToString();
40
41 //若有下级目录,对应其他图片
42 if (tn3.Tag.ToString() == "002001" || tn3.Tag.ToString() == "002002" || tn3.Tag.ToString() == "007001" || tn3.Tag.ToString() == "007002" || tn3.Tag.ToString() == "007003" || tn3.Tag.ToString() == "007004")
43 {
44 tn3.ImageIndex = 3;
45 }
46 else
47 {
48 tn3.ImageIndex = 2;
49 tn3.SelectedImageIndex = 2;
50 }
51 tn2.Nodes.Add(tn3);
52 //查找编号长度为3选项——四级
53 string SqlCY = "select * from YXL_TreeViewInfo where RP_Code like '" + tn3.Tag + "%' and LEN(RP_Code)=9";
54 ds = c.TreeView(SqlCY);
55 DataView dv4 = new DataView();
56 dv4.Table = ds.Tables[0];
57 foreach (DataRowView drv4 in dv4)
58 {
59 TreeNode tn4 = new TreeNode();
60 tn4.Tag = drv4["RP_Code"].ToString();
61 tn4.Text = drv4["RP_Name"].ToString();
62 tn4.ToolTipText = drv4["TableName"].ToString();
63 tn4.ImageIndex = 2;
64 tn4.SelectedImageIndex = 2;
65 tn3.Nodes.Add(tn4);
66
67 }
68 }
69 }
70
71 用这种方法属于很死的方法。当添加更多的子级时,需要手动再添加。可以点击根节点,动态绑定。
72
73 DataSet ds = new DataSet();
74 ds = server.ExecSqlForDataSet(Sql);
75
76 DataView dr1 = new DataView();
77 dr1.Table = ds.Tables[0];
78
79 foreach (DataRowView drv1 in dr1)
80 {
81
82 TreeNode tn1 = new TreeNode(); //实例化节点
83
84 tn1.Tag = drv1["RP_Code"].ToString(); //获取编号
85
86 tn1.Text = drv1["RP_Name"].ToString();//获取名称
87
88 tn1.ImageIndex = Convert.ToInt16(drv1["ImageIndex"].ToString());//获取图片索引
89
90 tn1.SelectedImageIndex = Convert.ToInt16(drv1["SelectedImageIndex"].ToString());//被选中图片索引
91
92 tn1.ToolTipText = drv1["TableName"].ToString();//表名
93
94 if (p_Tn.Nodes.Count == 0)//判断是否存在
95 {
96 p_Tn.Nodes.Add(tn1);//没有子节点,父级目录添加子级目录
97 }
98 else
99 {
100 bool IsNode = false;
101 //存在子节点
102 foreach (TreeNode item in p_Tn.Nodes)
103 {
104 if (item.Tag.Equals(tn1.Tag))
105 {
106 IsNode = true;
107 }
108 }
109 if (IsNode == false)
110 {
111 p_Tn.Nodes.Add(tn1);
112 }
113 }