Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
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 ); 

            tnChild1.ChildNodes.Add(tnChild2); 
            tnChild.ChildNodes.Add(tnChild1);
            tn.ChildNodes.Add(tnChild); 
            TreeView1.Nodes.Add(tn);


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
Comments
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.

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
    {
        [STAThread]
        static void Main()
        {
            // Create test Form
            Form form = new Form();
            // ... and add a TreeView to it
            TreeView treeview = new TreeView() { Dock = DockStyle.Fill };
            form.Controls.Add(treeview);
            
            // Create a root node
            TreeNode nodeRoot = new TreeNode("root");
            treeview.Nodes.Add(nodeRoot);

            // Add some child node trees to the root node with different nesting depths
            AddTreeNodesRecursive(nodeRoot, 4);
            AddTreeNodesRecursive(nodeRoot, 9);
            AddTreeNodesRecursive(nodeRoot, 3);

            // 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");
                nodeParent.Nodes.Add(nodeChild);

                // Call this function again, but now the child node is the new parent and the depth is decremented.
                AddTreeNodesRecursive(nodeChild, --iDepth); // recursion !
            }
        }
    }
}


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.

Good luck with your project!

kind regards Johannes
 
Share this answer
 
Comments
JChrisCompton 2-Jan-18 10:13am    
"... if you fear StackOverflow"
isn't that why we're on CodeProject instead? ;-)
Hi Just paste " the add statements " into a function.
ie,

private void add_child_node(string NodeName)
{
TreeNode tn = new TreeNode(NodeName+ ".1" );
tn.ChildNodes.Add(tnChild);
TreeView1.Nodes.Add(tn);
}

Call this function whenever the user needs to add the node(Push Button)
 
Share this answer
 
Comments
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.
private void add_child_node(TreeNode NodeName)
{
TreeNode tn = new TreeNode(NodeName.text+ ".1" );
NodeName.ChildNodes.Add(tn);
}

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
private void add_child_node(TreeNode NodeName,int noofnodes)
{
string temp=NodeName.Text;
for(int i=0;i<noofnodes;i++)>
{
temp += ".1";
TreeNode tn = new TreeNode(temp );
NodeName.ChildNodes.Add(tn);
}
}

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900