How to add a child node to a specific node in treeview? Say I have "Item1" in treeview already, how do I add "SubItem1" to "Item1" as it's child node?
I know its probably really simple, but i tried lots of stuff, i just cant get it working.
How to add a child node to a specific node in treeview? Say I have "Item1" in treeview already, how do I add "SubItem1" to "Item1" as it's child node?
I know its probably really simple, but i tried lots of stuff, i just cant get it working.
*Assumes empty TreeView:
I was looking for the same thing when i got here, and so far i couldnt get to what i needed.
So i got to this page: http://www.dotnetspider.com/forum/168335-How-add-node-treeview-VB.NET.aspx
Really cool and simple to do after you give it a look.
It turn out that we only need to keep typing nodes.add("nodename") to keep adding sublevels. Treeview1.Nodes.Add("Root_1").Nodes.Add("Child_Level_1").Nodes.Add("Child_Level_2")
This would get something like:
http://img716.imageshack.us/img716/7254/semttulonzk.jpg
Hope it Helped ;D.
Adding child node to parent (non-selected)
First use
Find()
to get a reference to the parent node. Then add it using the same technique as the other sections below.Adding nodes programmatically
If you want to add the child nodes to a particluar parent node, the idea is to add the child nodes to their parent node by using the
parent.node.add()
method. You can create any number of child like this.For example if you want to have a scenario like:
Then you could do this:
More reading/examples
This page has a good example you can run to dynamically add child nodes to the tree. They do it on a button, which they've hooked up like this:
http://www.codeproject.com/Articles/11830/The-Basic-Operations-on-using-the-TreeView-Control
If you make sure that you assign a
Name
to yourTreeNode
You can useFind
to locate it and add theChild
node.Example: