如何动态填充树视图(C#) [英] How to dynamically populate treeview (C#)

查看:174
本文介绍了如何动态填充树视图(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列,ID,名称和PARENTID表。 ID列包含运行数也作为主键。 ID也将是节点的名称属性。名称列包含字符串,将成为树节点的文本属性,而PARENTID是一列包含该节点的父ID。

I have a table with 3 columns, ID, Name and ParentID. The ID column contains running number which also serves as the primary key. ID will also be the node's Name properties. Name column contain string that will be the treenode's Text attribute, while ParentID is a column contains the node's parent ID.

这是我的表的样子:

ID     Name   ParentID
======================
1      A      0
2      A1     1
3      B      0
4      C      0
5      A2     1
6      B1     3

这表显示节点A是节点A1和A2的父节点。 PARENTID等于0表示该节点的父是根节点(硬编码)。例如,节点A,B和C是根节点的孩子。

This table shows that node A is the parent node for node A1 and A2. ParentID equals to "0" means that the node's parent is the root node (hardcoded). E.g., Node A, B and C are root node's children.

我填充树视图之前由PARENTID对行进行排序。我填充使用这两种方法(这里TreeNode的节点是被填充到树在childNode)树视图:

I sort the rows by ParentID prior to populating the treeview. I populate the tree view using these two methods (TreeNode node here is the childnode that is being populated into the tree):

    private void SearchParent(TreeView tree, String parentID, TreeNode node)
    {
        // Post: call TraverseParent method to search parent

        TreeNodeCollection collection = tree.Nodes;

        // Search parent recursively
        foreach (TreeNode n in collection)
        {
            TraverseParent(n, parentID, node);
        }
    }

    private void TraverseParent(TreeNode potentialParent, String parentID, TreeNode node)
    {
        // Post: search for parent. When parent is found add child to parent

        // am i the parent that you're looking for?
        if (parentID.CompareTo(potentialParent.Name) == 0)
        {
            // found me! i'm your parent!

            // add child to parent
            potentialParent.Nodes.Add(node);

            // update that the parent for child has been found
            parentFound = true;
        }
        else
        {
            // i'm not your parent

            // continue to look for parent recursively
            foreach (TreeNode n in potentialParent.Nodes)
            {
                TraverseParent(n, parentID, node);
            }
        }
    }



一切都很好,直到我拖通过节点的节点C的孩子 - 和拖放节点和提交更改到数据库。

All is well until I drag-and-drop the nodes by making node A the child of node C and commit the changes to the database.

现在我的数据库表如下所示:

Now my database table looks like this:

ID     Name   ParentID
======================
1      A      4
2      A1     1
3      B      0
4      C      0
5      A2     1
6      B1     3

下一次我运行应用程序时,它未能填充节点A1和A2成树,因为它找不到自己的父母。这是因为当我填充树视图之前基于PARENTID行进行排序,行排序是这样的:

The next time I run the application, it fails to populate node A1 and A2 into the tree because it could not find their parents. This is because when I sort the rows based on ParentID prior to populating the treeview, the rows are sorted like this:

ID     Name   ParentID
======================
3      B      0
4      C      0
2      A1     1
5      A2     1
6      B1     3
1      A      4

这方法,我的应用程序会尝试填充A1和A2节点到创建节点A甚至在树。因此,应用程序无法找到节点A1和A2的父。

This way, my application will try to populate A1 and A2 nodes into the tree even before node A is created. Therefore the application could not find the parent for node A1 and A2.

因此​​,谁能告诉我一个方法来修复这个bug或者有更好的方式来动态填充一个TreeView?

Hence, can anyone tell me a way to fix this bug or is there a better way to dynamically populate a treeview?

感谢。

推荐答案

您应该使用递归。填充

承诺的例子:

public partial class Form1 : Form
    {
        private class ItemInfo
        {
            public int ID;
            public int ParentID;
            public string Name;
        }

        public Form1()
        {
            InitializeComponent();
            FillTreeView();
        }

        private void FillTreeView()
        {
            var items = new List<ItemInfo>()
            {
                new ItemInfo(){ID = 1, ParentID = 4, Name = "A"},
                new ItemInfo(){ID = 2, ParentID = 1, Name = "A1"},
                new ItemInfo(){ID = 3, ParentID = 0, Name = "B"},
                new ItemInfo(){ID = 4, ParentID = 0, Name = "C"},
                new ItemInfo(){ID = 5, ParentID = 1, Name = "A2"},
                new ItemInfo(){ID = 6, ParentID = 3, Name = "B1"},
            };

            FillNode(items, null);
        }

        private void FillNode(List<ItemInfo> items, TreeNode node)
        {
            var parentID = node != null
                ? (int)node.Tag
                : 0;

            var nodesCollection = node != null
                ? node.Nodes
                : treeView1.Nodes;

            foreach (var item in items.Where(i => i.ParentID == parentID))
            {
                var newNode = nodesCollection.Add(item.Name, item.Name);
                newNode.Tag = item.ID;

                FillNode(items, newNode);
            }
        }
    }

这篇关于如何动态填充树视图(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆