Version

Add and Remove Nodes Programmatically

This topic will show you how to add and remove nodes at run-time. The Add and Remove methods of the TreeNodesCollection object are used for this purpose. To add a child node the Add method has to be used on the parent node.

In Visual Basic:

Imports Infragistics.Win.UltraWinTree
...
' Declare nodes
Dim nodeA As UltraTreeNode
Dim nodeB As UltraTreeNode
' Add two nodes to the WinTree
nodeA = Me.ultraTree1.Nodes.Add("nodeA", "NodeA")
nodeB = Me.ultraTree1.Nodes.Add("nodeB", "NodeB")
' Expand the newly added node
nodeA.Expanded = True
' Add a child node to Node A
nodeA.Nodes.Add("childNodeofA", "ChildNodeA1")
' Add Node C to WinTree
Me.ultraTree1.Nodes.Add("nodeC", "NodeC")
' Get a reference to Node C using its key
Dim nodeC As UltraTreeNode = Me.ultraTree1.GetNodeByKey("nodeC")
' Add a child node to NodeC
nodeC.Nodes.Add("childNodeofC", "ChildNodeC1")
' Remove NodeB
Me.ultraTree1.Nodes.Remove(nodeB)

In C#:

using Infragistics.Win.UltraWinTree;
...
 // Declare nodes
 UltraTreeNode nodeA;
 UltraTreeNode nodeB;
 // Add two nodes to the WinTree
 nodeA = this.ultraTree1.Nodes.Add("nodeA","NodeA");
 nodeB = this.ultraTree1.Nodes.Add("nodeB", "NodeB");
 // Expand the newly added node
 nodeA.Expanded = true;
 // Add a child node to Node A
 nodeA.Nodes.Add("childNodeofA", "ChildNodeA1");
 // Add Node C to WinTree
 this.ultraTree1.Nodes.Add("nodeC", "NodeC");
 // Get a reference to Node C using its key
 UltraTreeNode nodeC = this.ultraTree1.GetNodeByKey("nodeC");
 // Add a child node to NodeC
 nodeC.Nodes.Add("childNodeofC", "ChildNodeC1");
 // Remove NodeB
 this.ultraTree1.Nodes.Remove(nodeB);