john true
john true

Reputation: 273

Adding childnode to childnode using treeview

I want to add child node to child node.

I use below code;

treeView1.Nodes.Add("0");
treeView1.Nodes["0"].Nodes.Add("1");
treeView1.Nodes["1"].Nodes.Add("2");
treeView1.Nodes["2"].Nodes.Add("3");
treeView1.Nodes["3"].Nodes.Add("4");
treeView1.Nodes["1"].Nodes.Add("5");

What i want is this;

0
 1
  2
   3
    4
  5

However it always fail. It doesn' find the parent nodes. What should i do ?

Upvotes: 0

Views: 4316

Answers (3)

David Fletcher
David Fletcher

Reputation: 305

The issue that you are having is that the Nodes collection of the treeView1 control is not a list of all the nodes in the tree - it is only a list of the top level nodes. What makes it hierarchical is that a tree node can contain a list of child nodes. So, to get the tree structure you defined in your question, you will want to do something like this:

private void AddTreeNodes()
{
    // Create the root node.
    TreeNode node = treeView1.Nodes.Add("0");

    // the root node has only one child node (1), so we will reuse the node object
    node = node.Nodes.Add("1");

    // Now, the node is the parent of two nodes (2 and 5).
    // We will want to keep track of node 2 because it has child nodes.
    TreeNode anotherNode = node.Nodes.Add("2");

    // Since node 5 doesn't have any child nodes, we don't need to keep track of it in this example.
    node.Nodes.Add("5");

    // Now, we need to make the child node of node 2. (Specifically, node 3).
    node = anotherNode.Nodes.Add("3");

    // And finally add the child node of node 3.
    node.Nodes.Add("4");
}

You could also do it like this:

private void AddTreeNodes()
{
    treeView1.Nodes.Add("0");
    treeView1.Nodes[0].Nodes.Add("1");
    treeView1.Nodes[0].Nodes[0].Nodes.Add("2");
    treeView1.Nodes[0].Nodes[0].Nodes.Add("5");
    treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add("3");
    treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes.Add("4");
}

Now, I know that the code is ugly, but the main ideas to get from this are:

  1. The TreeView control contains a List of TreeNode objects (Nodes). This list is not a list of all nodes within the tree, just those that are the direct children of the control itself.
  2. Any child nodes that exist under a parent node must be added to the parent node's list of TreeNode objects.
  3. Every TreeNode object contains a list of TreeNode objects that are it's children.
  4. When you add a TreeNode to another Node, or the TreeView control itself, a reference to the new TreeNode object is returned.
  5. The Index of a TreeNode is it's index in relation to the list that it is within - not an index in relation to the entire hierarchy.
  6. The TreeNode object has several properties that help with navigating the tree. PrevNode and NextNode get the previous or next sibling tree node (if they exist). and Parent gets the parent TreeNode of the current node.

Upvotes: 4

Chris Walsh
Chris Walsh

Reputation: 3523

I suspect that the problem is that you are using the TreeNodeCollection.Add method that accepts a string value which is the text of the node and not the key. You then try to populate other nodes via a key.

Try the following instead:

treeView1.Nodes.Add("0", "0");
treeView1.Nodes["0"].Add("1", "1");
treeView1.Nodes["0"].Nodes["1"].Add("2", "2");

Upvotes: 1

René Vogt
René Vogt

Reputation: 43946

It doesn't work because Nodes[string] is looking for a TreeNode in this collection with the specified key (which is not the node's text).

TreeNodeCollection.Add returns the inserted TreeNode. So use this return value to add further child nodes:

TreeNode child = treeView1.Nodes.Add("0");
TreeNode child1 = child.Nodes.Add("1");
child1.Nodes.Add("2").Nodes.Add("3").Nodes.Add("4");
child1.Nodes.Add("5");

Upvotes: 0

Related Questions