How to Add and Remove Nodes with the TreeView Control at runtime in .net

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

One thought on “How to Add and Remove Nodes with the TreeView Control at runtime in .net”

Comments are closed.