The Windows Forms TreeView control has the top-level nodes in its Nodes collection. Each TreeNode also has its own Nodes collection to store its child nodes.
you can add, remove, and rearrange the nodes at a single level of the node hierarchy.
Add nodes to treeView control programmatically
if you want to add new node as a child node of the currently selected node in treeview.
[vb]
Dim childNode As TreeNode = New TreeNode("New node") TreeView1.SelectedNode.Nodes.Add(childNode )
[c#]
TreeNode childNode = new TreeNode("New node"); treeView1.SelectedNode.Nodes.Add(childNode );
Remove nodes of treeView control programmatically
[vb]
if you want to remove selected node of the TreeView control.
TreeView1.Nodes.Remove(TreeView1.SelectedNode)
if you want to remove all nodes of the TreeView control.
TreeView1.Nodes.Clear()
[c#]
if you want to remove selected node of the TreeView control.
treeView1.Nodes.Remove(treeView1.SelectedNode);
if you want to remove all nodes of the TreeView control.
TreeView1.Nodes.Clear();
Awesome! Thank you so much for this post. I appreciate it! 🙂