TreeView Treenode problem RRS feed

  • Question

  • I have a root node for each of my supplies which i set the tag to Rootnode and the text to the  supply name.
    Then i have sub nodes off them that hold the units on hand for those items.

    My problem is i want to delete or remove the ROOT node of the supply if there is no units for them.
    I get Out of range errors...
    My code is basicly like this

    while (supplies )
       make a root node
         while (go through the supply database and see if it has something in it)
                if ( there are are units )
                           make another node
       if there arent.
    Remove the current root node...

    but it just don't work, I will try to post my excact code but I don't have any net access on that pc

    (Moderator: Moved to Windows Forms Forum)

    Tuesday, October 24, 2006 3:36 PM


  • If I were in your situation, I think the easiest solution would be to check if there were units before creating the root node itself.  If the root node is not going to exist if there are no sub items to list under it, then simply do not create the root node if there are no sub items.

    Another quick solution would be to store the root node in a TreeNode variable when you create it, and then call the Remove() method on it to remove it from the TreeView.

    I am not sure which method you are using to remove the root node if it is empty, but perhaps you are using indexing and using an index number that doesn't exist.

    TreeNode rootNode = new TreeNode();
    rootNode.Text = "I am the root node.";

    if (supplies.Count < 1)



    You might also be able to call treeView1.Remove(rootNode) as well.

    I hope this helps.

    Tuesday, October 24, 2006 9:06 PM