Skip to content

Add node to treeView control

kobi shaal

Asked Aug 17, 2007, 5:13 AM EDT5 answers
Share
LinkedInX

Hello there,

How can i add a node in a certian place in a treeView control?

 

Thanks ahead

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    Add will add at the end of the Nodes collection,

    The Insert method will add at a particular index in the Nodes collection

    e.g.

    myTreeView.Nodes.Insert(3, myTreeNode);

    0
  • 0

    how can i found the index which represnt my needed node?

    Thanks

    0
  • Mike Gold
    0

    I don't really understand the question.

    you could loop through the nodes under the parent and when you find the node you need to insert after or before, use the Insert

    Code
    for (int i = 0; i <  myTreeView.Nodes.Count; i++)
    {
       if (myTreeView.Nodes[i] == <whatever your condition is>
      {
                myTreeView.Nodes.Insert(i, myNewNode);
      }
    if you need to search a lower level, use the ChildNodes of a TreeNode the same way.
    
    
    }
    0
  • 0

    i did something like you said :

    until now im using a recursive function which travel over my tree and when the function found the needed function it reutun the founded node.

     

    now to this node i need to add  a node so it will like a child to the founed node

    how can i do it?

     

    Thanks again

    0
  • Mike Gold
    0

    foundNode.ChildNodes.Add 

    or

    foundNode.ChildNodes.Insert

    depending on where you want to put the child node

    0