遍历Treeview每个节点并初始化(C#)
搞了好久,哎,C#的一些控件用起来还没习惯,所以折腾啊。
TreeView的形成,必然要初始化,数据记录是从数据库中取得的,那么要先取再遍历。介绍下心得吧。
首先,数据预期显示结果如下
其次,数据库结构如下
需要解释的是,c_id为自增长的主键,也就是即将要用到的TV节点的TAG,c_pid是父节点的TAG
数据库内容如下
初始化显示的代码如下,用了递归思想
1 MySqlConnection mysqlconn = null;
2 MySqlCommand myCmd = new MySqlCommand();
3 MySqlDataReader myDr = null;//数据库连接
4 public usInstallAdd(MDIParent m, GlobalClass g)
5 {//构造函数
6 mdi = m;
7 globalvar = g;
8 InitializeComponent();
9 tv_Pro.Dock = DockStyle.Left;
10
11 //load data
12 mysqlconn = new MySqlConnection(globalvar.strmysqlconnstr);
13 mysqlconn.Open();
14 myCmd.Connection = mysqlconn;
15 string strSQl = @"select *from t_pro";
16 myCmd.CommandText = strSQl;
17 myCmd.CommandType = CommandType.Text;
18 myDr = myCmd.ExecuteReader();
19
20 TreeNode pnode = new TreeNode();
21 pnode.Text = "项目管理";
22 pnode.Tag = 0;
23 tv_Pro.Nodes.Add(pnode);//先初始化一个节点
24
25 while (myDr.Read())
26 {
27 TreeNode node = new TreeNode();
28 node.Text = myDr["c_name"].ToString();
29 node.Tag = myDr.GetInt32(0);
30 string strValue = myDr["c_pid"].ToString();
31 if (pnode != null)
32 {//遍历查询并添加
33 fun_FindNode(pnode, strValue, node);
34 }
35 }
36 tv_Pro.ExpandAll();
37 myDr.Close();
38 myCmd.Dispose();
39 mysqlconn.Close();
40 }
41 //遍历函数如下
42 private TreeNode fun_FindNode(TreeNode tnParent, string strValue, TreeNode tnc)
43 {
44 //遍历TV
45 if (tnParent == null)
46 return null;
47 if (tnParent.Tag.ToString() == strValue)
48 {
49 tnParent.Nodes.Add(tnc);
50 return tnParent;
51 }
52 TreeNode tnRet = null;
53 foreach (TreeNode tn in tnParent.Nodes)
54 {
55 tnRet = fun_FindNode(tn, strValue, tnc);
56 if (tnRet != null)
57 {
58 //tnRet.Nodes.Add(tnc);
59 break;
60 //continue;
61 }
62 }
63 return tnRet;
64 }
2 MySqlCommand myCmd = new MySqlCommand();
3 MySqlDataReader myDr = null;//数据库连接
4 public usInstallAdd(MDIParent m, GlobalClass g)
5 {//构造函数
6 mdi = m;
7 globalvar = g;
8 InitializeComponent();
9 tv_Pro.Dock = DockStyle.Left;
10
11 //load data
12 mysqlconn = new MySqlConnection(globalvar.strmysqlconnstr);
13 mysqlconn.Open();
14 myCmd.Connection = mysqlconn;
15 string strSQl = @"select *from t_pro";
16 myCmd.CommandText = strSQl;
17 myCmd.CommandType = CommandType.Text;
18 myDr = myCmd.ExecuteReader();
19
20 TreeNode pnode = new TreeNode();
21 pnode.Text = "项目管理";
22 pnode.Tag = 0;
23 tv_Pro.Nodes.Add(pnode);//先初始化一个节点
24
25 while (myDr.Read())
26 {
27 TreeNode node = new TreeNode();
28 node.Text = myDr["c_name"].ToString();
29 node.Tag = myDr.GetInt32(0);
30 string strValue = myDr["c_pid"].ToString();
31 if (pnode != null)
32 {//遍历查询并添加
33 fun_FindNode(pnode, strValue, node);
34 }
35 }
36 tv_Pro.ExpandAll();
37 myDr.Close();
38 myCmd.Dispose();
39 mysqlconn.Close();
40 }
41 //遍历函数如下
42 private TreeNode fun_FindNode(TreeNode tnParent, string strValue, TreeNode tnc)
43 {
44 //遍历TV
45 if (tnParent == null)
46 return null;
47 if (tnParent.Tag.ToString() == strValue)
48 {
49 tnParent.Nodes.Add(tnc);
50 return tnParent;
51 }
52 TreeNode tnRet = null;
53 foreach (TreeNode tn in tnParent.Nodes)
54 {
55 tnRet = fun_FindNode(tn, strValue, tnc);
56 if (tnRet != null)
57 {
58 //tnRet.Nodes.Add(tnc);
59 break;
60 //continue;
61 }
62 }
63 return tnRet;
64 }