15,917,061 members
5.00/5 (1 vote)
See more:
Hi All

I need to Add Child and Grandchild to the parentnode dynamically for a treeview as shown below:

Treeview
-->Parent Node
--> Child Node
--> GrandChild Node
--> GrandGrandChild Node

C#
```string NodeName = "Node - 1";
TreeNode tn = new TreeNode(NodeName );

NodeName = NodeName + ".1";
TreeNode tnChild  = new TreeNode(NodeName );

NodeName = NodeName + ".1";
TreeNode tnChild1 = new TreeNode(NodeName );

NodeName = NodeName + ".1";
TreeNode tnChild2 = new TreeNode(NodeName );

The above code means if the depth = 4 this should happen if the depth = 3 it should stop till GrandChildNode.. For this I need a dynamic code in c#
Posted
Ian A Davidson 30-Apr-13 3:59am
So why don't you do it then? I don't understand the problem.
Mathi2code 30-Apr-13 4:33am
The point is the depth may be 3 or 4 or 5 or n that comes from user so based on that I need to create 'n' no. of nodes dynamically.

## Solution 1

Hi Mathi2code,

I'd use a recursive initialization function. something like this: (you can run the example)

```using System;
using System.Windows.Forms;

namespace RecursiveTreeViewNodes
{
static class Program
{
static void Main()
{
// Create test Form
Form form = new Form();
// ... and add a TreeView to it
TreeView treeview = new TreeView() { Dock = DockStyle.Fill };

// Create a root node
TreeNode nodeRoot = new TreeNode("root");

// Add some child node trees to the root node with different nesting depths

// Show test Form
Application.Run(form);
}

static void AddTreeNodesRecursive(TreeNode nodeParent, int iDepth)
{
if (iDepth > 0 && nodeParent != null)
{
// Create one child node for the given parent node
TreeNode nodeChild = new TreeNode(nodeParent.Text + ".1");

// Call this function again, but now the child node is the new parent and the depth is decremented.
}
}
}
}```

This should give you just the General idea. If if you expect very deep nesting depths you should avoid recursion, it could be reforumlated to a loop if you are fearing StackOverflows.

kind regards Johannes

JChrisCompton 2-Jan-18 10:13am
"... if you fear StackOverflow"
isn't that why we're on CodeProject instead? ;-)

## Solution 2

Hi Just paste " the add statements " into a function.
ie,

{
TreeNode tn = new TreeNode(NodeName+ ".1" );
}

Call this function whenever the user needs to add the node(Push Button)

johannesnestler 30-Apr-13 10:55am
Yes, I think this could also be a solution for OP (hard to tell if he want's to add nodes "automatic" or the user adds the nodes. What I don't like is your example (what is tnChild?, why not giving the parent node as parameter etc.) - BUT I think it would be good enough for me to get the idea - let's see what Mathi2code says...
Member 10012743 30-Apr-13 11:05am
sorry johan,
i just cut and paste the question. No need of that statement.
{
TreeNode tn = new TreeNode(NodeName.text+ ".1" );
}

I think you would be more clear now.
Just send the parent node to function.
and if he needs to create n no. of child nodes, he would better use this in a loop function [for, do-while].
Mathi2code 2-May-13 4:31am
I want it to be loaded automatic....In that case will this work ?
siri chandu 20-Apr-17 7:13am
hi,,,,, friends
i want to add the nodes to tree view externally

## Solution 3

{
string temp=NodeName.Text;
for(int i=0;i<noofnodes;i++)>
{
temp += ".1";
TreeNode tn = new TreeNode(temp );
}
}

where noofnodes is the no. of nodes you(or user) needs to add to the parent