Actually your code should work - to add a sub node, you just need to do:
myNode.Nodes.Add(new TreeNode("Sub node"));
Perhaps the problem is how you reference existing nodes. I assume tree.Nodes [item.Name] returned null?
For this indexer to find the node, you need to specify the key when adding the node. Did you specify the name node as the key? For example, the following code works for me:
treeView1.Nodes.Add("key", "root"); treeView1.Nodes["key"].Nodes.Add(new TreeNode("Sub node"));
If my answer doesn't work, can you add more details about what is happening? Did you get some kind of exception or just nothing happened?
PS: to store an object in node, instead of using the Tag property, you can also get your own class from TreeNode and save something in it. If you are developing a library, this is more useful as you leave the Tag property for your users.
Ran
source share