TreeView for WinForms | ComponentOne
Node Operations / Add Nodes
In This Topic
    Add Nodes
    In This Topic

    Adding Nodes

    The C1TreeNodeCollection class provides the Add method and the Insert method for adding nodes in TreeView. To add a parent node, you can use either of the two methods with the Nodes collection of the C1TreeView class. Next, to add a child node in the parent node, or children nodes in a child node, you can either use the Add method or the Insert method with the Nodes collection of the C1TreeNode class.

    The following code snippet demonstrates how to add nodes in the TreeView control using the Add method.

    ' create parent nodes
    Dim parentNode1 As New C1.Win.TreeView.C1TreeNode()
    Dim parentNode2 As New C1.Win.TreeView.C1TreeNode()
    
    ' add the parent nodes to the TreeView Nodes collection
    C1TreeView1.Nodes.Add(parentNode1)
    C1TreeView1.Nodes.Add(parentNode2)
    
    ' set the values of the parent nodes
    parentNode1.SetValue("Parent1", 0)
    parentNode2.SetValue("Parent2", 0)
    
    ' create child nodes
    Dim childNode1 As New C1.Win.TreeView.C1TreeNode()
    Dim childNode2 As New C1.Win.TreeView.C1TreeNode()
    
    ' add the child nodes to the parent nodes' Nodes collection
    parentNode1.Nodes.Add(childNode1)
    parentNode2.Nodes.Add(childNode2)
    
    ' set the values of the child nodes
    childNode1.SetValue("Child1", 0)
    childNode2.SetValue("Child2", 0)
    
    // create parent nodes
    C1.Win.TreeView.C1TreeNode parentNode1 = new C1.Win.TreeView.C1TreeNode();
    C1.Win.TreeView.C1TreeNode parentNode2 = new C1.Win.TreeView.C1TreeNode();
    
    // add the parent nodes to the TreeView Nodes collection
    c1TreeView1.Nodes.Add(parentNode1);
    c1TreeView1.Nodes.Add(parentNode2);
    
    // set the values of the parent nodes
    parentNode1.SetValue("Parent1", 0);
    parentNode2.SetValue("Parent2", 0);
    
    // create child nodes
    C1.Win.TreeView.C1TreeNode childNode1 = new C1.Win.TreeView.C1TreeNode();
    C1.Win.TreeView.C1TreeNode childNode2 = new C1.Win.TreeView.C1TreeNode();
    
    // add the child nodes to the parent nodes' Nodes collection
    parentNode1.Nodes.Add(childNode1);
    parentNode2.Nodes.Add(childNode2);
    
    // set the values of the child nodes
    childNode1.SetValue("Child1", 0);
    childNode2.SetValue("Child2", 0);
    

    The following image shows the treeview after adding nodes.

    TreeView with added nodes

    The following code snippet demonstrates how to add nodes in the TreeView control using the Insert method.

    ' create third parent node
    Dim parentNode3 As New C1.Win.TreeView.C1TreeNode()
    
    ' insert the third parent node in the TreeView Nodes collection
    C1TreeView1.Nodes.Insert(2, parentNode3)
    
    ' set the value of the third parent node
    parentNode3.SetValue("Parent3", 0)
    
    ' create third child node
    Dim childNode3 As New C1.Win.TreeView.C1TreeNode()
    
    ' insert the third child node in the third parent node's Nodes collection
    parentNode3.Nodes.Insert(0, childNode3)
    
    ' set the value of the third child node
    childNode3.SetValue("Child3", 0)
    
    // create third parent node
    C1.Win.TreeView.C1TreeNode parentNode3 = new C1.Win.TreeView.C1TreeNode();
    
    // insert the third parent node in the TreeView Nodes collection
    c1TreeView1.Nodes.Insert(2, parentNode3);
    
    // set the value of the third parent node
    parentNode3.SetValue("Parent3", 0);
        
    // create third child node
    C1.Win.TreeView.C1TreeNode childNode3 = new C1.Win.TreeView.C1TreeNode();
    
    // insert the third child node in the third parent node's Nodes collection
    parentNode3.Nodes.Insert(0, childNode3);
    
    // set the value of the third child node
    childNode3.SetValue("Child3", 0);
    

    The final treeview appears, as shown below.

    TreeView with added nodes