使用动态子节点填充树视图

时间:2014-06-23 10:21:05

标签: c# treeview

我有两列的表格:

+-------------+------------+  
| Level       | Desc       |  
+-------------+------------+  
| 1           | a          |  
+-------------+------------+    
| 2           | b          |  
+-------------+------------+ 
| 2           | c          | 
+-------------+------------+    
| 1           | d          |  
+-------------+------------+ 
| 2           | e          | 
+-------------+------------+    
| 2           | f          |  
+-------------+------------+ 
| 3           | g          | 
+-------------+------------+    
| 1           | h          |  
+-------------+------------+ 
| 1           | i          | 
+-------------+------------+ 
| 2           | j          |  
+-------------+------------+ 
| 2           | k          | 
+-------------+------------+ 

我需要根据Level列在树视图中创建此数据的显示 应该像:

- 1 a
   -- 2 b
   -- 2 c
 -1 d
   -- 2 e
   -- 2 f
      -- 3 g
 -1 h
 -1 i 
   -- 2 j 
   -- 2 k

因此,级别列表示节点。级别1是主节点,级别2是级别1的子节点,级别3是级别2的子节点,级别4是子节点3 ... 我知道如果有固定数量的节点和子节点,如何填充树视图,但在这种情况下,不知道如何填充1个节点由3个,4个或更多个子节点组成的位置。

1 个答案:

答案 0 :(得分:3)

希望它会对你有所帮助,我尝试了你的例子并且它成功地运作了

    private void PopulateTreeView_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Level");
        table.Columns.Add("Data");

        table.Rows.Add(1, "a");
        table.Rows.Add(2, "b");
        table.Rows.Add(2, "c");
        table.Rows.Add(1, "d");
        table.Rows.Add(2, "e");
        table.Rows.Add(2, "f");
        table.Rows.Add(3, "g");
        table.Rows.Add(4, "z");
        table.Rows.Add(5, "x");
        table.Rows.Add(2, "h");
        table.Rows.Add(3, "i");
        table.Rows.Add(1, "j");
        table.Rows.Add(2, "k"); 

        TreeNode lastNode = new TreeNode();    

        for (int i = 0; i < table.Rows.Count; i++)
        {
            TreeNode newNode = new TreeNode((string)table.Rows[i]["Data"]);                

            if (i == 0)
                treeView.Nodes.Add(newNode);
            else
            {
                int currentLevel = Convert.ToInt32(table.Rows[i]["Level"]);
                int lastLevel = Convert.ToInt32(table.Rows[i-1]["Level"]);

                if (currentLevel < lastLevel)
                {
                    if (currentLevel == 1)
                        treeView.Nodes.Add(newNode);
                    else
                    {
                        TreeNode currentNode = lastNode.Parent;

                        for (int j = 0; j < lastLevel - currentLevel; j++)
                            currentNode = currentNode.Parent;

                        currentNode.Nodes.Add(newNode);
                    }
                }
                else if (currentLevel == lastLevel)
                {
                     if (lastLevel == 1)
                        treeView.Nodes.Add(newNode);
                     else
                        lastNode.Parent.Nodes.Add(newNode);                                    
                }
                else
                {
                    lastNode.Nodes.Add(newNode);
                }
            }

            lastNode = newNode;
        }           
    }

enter image description here