Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 

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 29-Apr-13 22:48pm
Comments
Ian A Davidson at 30-Apr-13 3:59am
   
So why don't you do it then? I don't understand the problem.
Mathi2code at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

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
    {
        [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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
  Permalink  
Comments
johannesnestler at 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 at 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 at 2-May-13 4:31am
   
I want it to be loaded automatic....In that case will this work ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 400
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 245
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100