tags:

views:

80

answers:

4

I have a String List with items like this

"Root"

"Root/Item1"

"Root/Item2"

"Root/Item3/SubItem1"

"Root/Item3/SubItem2"

"Root/Item4/SubItem1"

"AnotherRoot"

How do I transfer this stringlist into a treeview ?

+1  A: 

You can split each item into it's substrings. Then via recursion look for each item, if the parent exists add to it, and if the parent doesn't exists create it.

If you can't see how to do it, i`ll post you a sample code

Sample Code

    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));
            }
        }
    }

And the caller example:

        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);
        }
Am
a sample code would be very much appreciated
tomfox66
A: 

One way is to iterate the items split the item and push them on a list and if the parent doesn't match pop an item from the list until the stack is empty or you have a match.

orjan
A: 

You can use this code:

    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);
    }

Hope this helps.

Ricardo Lacerda Castelo Branco

Ricardo Lacerda Castelo Branco
A: 

Thanks to all

your answers were -as usual- very helpful

tomfox66
@tomfox - you've managed to create two accounts. This one (http://stackoverflow.com/users/198332/tomfox) and the one used to ask the question (http://stackoverflow.com/users/197692/tomfox). Contact [email protected] about getting them merged.
ChrisF