将stringlist转换为treeview

时间:2009-10-27 22:29:38

标签: c# treeview

我有一个包含这样的项目的字符串列表

“根”

“根/ Item1的”

“根/项目2”

“根/项目3 /的SubItem1”

“根/项目3 / SubItem2”

“根/项目4 /的SubItem1”

“AnotherRoot”

如何将此字符串列表传输到树视图中?

3 个答案:

答案 0 :(得分:2)

您可以将每个项目拆分为其子字符串。然后通过递归查找每个项目,如果父项存在添加到它,如果父项不存在,则创建它。

如果你看不懂怎么做,我会给你发一个示例代码

示例代码

    public void AddItem(TreeView treeControl, TreeNode parent, string item)
    {
        TreeNodeCollection nodesRef = (parent != null) ? parent.Nodes : treeControl.Nodes;
        string currentNodeName;
        if (-1 == item.IndexOf('/')) currentNodeName = item;
        else  currentNodeName = item.Substring(0, item.IndexOf('/'));
        if (nodesRef.ContainsKey(currentNodeName))
        {
            AddItem(treeControl, nodesRef[currentNodeName], item.Substring(currentNodeName.Length+1));
        }
        else
        {
            TreeNode newItem = nodesRef.Add(currentNodeName, currentNodeName);
            if (item.Length > currentNodeName.Length)
            {
                AddItem(treeControl, newItem, item.Substring(item.IndexOf('/', currentNodeName.Length) + 1));
            }
        }
    }

来电者的例子:

        string[] stringArr = {
                                 "Root",
                                 "Root/Item1",
                                 "Root/Item2",
                                 "Root/Item3/SubItem1",
                                 "Root/Item3/SubItem2",
                                 "Root/Item4/SubItem1",
                                 "AnotherRoot"
                             };
        foreach (string item in stringArr)
        {
            AddItem(treeView1, null, item);
        }

答案 1 :(得分:0)

一种方法是迭代分割项目的项目并将它们推送到列表中,如果父项不匹配则弹出列表中的项目,直到堆栈为空或者您有匹配项。

答案 2 :(得分:0)

您可以使用此代码:

    private void button1_Click(object sender, EventArgs e) {
        List<String> paths = new List<String> { 
            "Root", "Root/Item1", "Root/Item2", "Root/Item3/SubItem1", 
            "Root/Item3/SubItem2", "Root/Item4/SubItem1", "AnotherRoot"
        };

        List<TreeNode> nodeCollection = new List<TreeNode>();

        foreach (var path in paths) {
            AddPath(nodeCollection, path);
        }

        treeView1.Nodes.Clear();
        treeView1.Nodes.AddRange(nodeCollection.ToArray());
    }

    public void AddPath(List<TreeNode> collection, String path) {
        LinkedList<String> pathToBeAdded = new LinkedList<String>(path.Split(new String[] { @"/" }, StringSplitOptions.RemoveEmptyEntries));

        if (pathToBeAdded.Count == 0) {
            return;
        }

        String rootPath = pathToBeAdded.First.Value;
        TreeNode root = collection.FirstOrDefault(n => n.Text.Equals(rootPath));

        if (root == null) {
            root = new TreeNode(rootPath);
            collection.Add(root);
        }

        pathToBeAdded.RemoveFirst();
        AddPath(root, pathToBeAdded);
    }

    public void AddPath(TreeNode rootNode, LinkedList<String> pathToBeAdded) {
        if (pathToBeAdded.Count == 0) {
            return;
        }

        String part = pathToBeAdded.First.Value;
        TreeNode subNode = null;

        if (!rootNode.Nodes.ContainsKey(part)) {
            subNode = rootNode.Nodes.Add(part, part);
        } else {
            subNode = rootNode.Nodes[part];
        }

        pathToBeAdded.RemoveFirst();
        AddPath(subNode, pathToBeAdded);
    }

希望这有帮助。

Ricardo Lacerda Castelo Branco