WinForm 數據庫無限填充樹目錄 treeView


 

我自己想的是處理數據庫每一條數據,然后來插入子節點的子節點。

奈何沒有插入子節點的子節點的辦法,百度來百度去,一看全都是遞歸。

本來我是絕望的,

但是沒辦法,老板的需求不能駁回啊,於是就來ctrl c/ctrl v吧。

在網上查到了一個能看懂的,基本是原樣copy了下來。

 

 1         private void SetTreeView()
 2         {
 3             //填充treeview
 4             string sql = "select * from SupplierType";
 5             DataTable dt = SQLHelp.GetDataTable(sql);
 6             dataGridView.DataSource = dt;
 7 
 8             if (dt != null)
 9             {
10                 foreach (DataRow row in dt.Rows)
11                 {
12                     string id = row["SupplierTypeID"].ToString().Trim();
13                     string name = "(" + id + ")" + row["SupplierTypeName"].ToString().Trim();
14                     string fatherid = row["SupplierFatherTypeID"].ToString().Trim();
15 
16                     TreeNode node = new TreeNode(name);
17                     node.Tag = id;
18                     //在樹中根據id來查找這個節點,如果沒有找到,則說明這個節點是父節點,如果查找到這個節點,則返回,返回的節點為node節點的父節點
19                     TreeNode parentNode = GetNodeByID(treeView, fatherid);
20                     if (parentNode == null)
21                     {
22                         treeView.Nodes.Add(node);
23                     }
24                     else
25                     {
26                         parentNode.Nodes.Add(node);
27                     }
28                 }
29             }
30  
31         }
32 
33         
34         private TreeNode GetNodeByID(TreeView treeView, string fatherid)
35         {
36             //根據id搜索節點
37             TreeNode result = null;
38             foreach (TreeNode node in treeView.Nodes)
39             {
40                 if (node.Tag.ToString() == fatherid)
41                 {
42                     result = node;
43                     break;
44                 }
45                 if (node.Nodes.Count > 0)
46                 {
47                     result = GetNodeByID(node, fatherid);
48                     if (result != null)
49                     { break; }
50                 }
51             }
52             return result;
53         }
54 
55         private TreeNode GetNodeByID(TreeNode parentNode, string fatherid)
56         {
57             //根據節點搜索子節點的節點
58             TreeNode result = null;
59             foreach (TreeNode node in parentNode.Nodes)
60             {
61                 if (node.Tag.ToString() == fatherid)
62                 {
63                     result = node;
64                     break;
65                 }
66                 if (node.Nodes.Count > 0)
67                 {
68                     result = GetNodeByID(node, fatherid);
69                     if (result != null)
70                     { break; }
71                 }
72             }
73             return result;
74         }

 

原理就是遞歸去尋找父元素,將加載好的節點向上層添加,

數據庫中重點的數據就是,自己的ID,父類的ID,以及內容。

 

成果:

 

 

參考:http://www.cnblogs.com/wangshuai/archive/2010/07/21/1782522.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM