Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm attempting to dynamically build a treeview from data that I'm pulling out of a database. Some parent nodes have a child node while others do not, and some have a grand child while other do not. I'm attempting to use the code below but I'm getting a compiler error "Specified argument was out of range of valid values." I've tried few different ways of doing this but no luck. Thanks for you help.

Here's an example of the data

CategoryName   SubCategoryName   SubCategory2Name 
ASV            G-1                 Error 
ASV            SV                  Error 
LYT     
INS            Script   
INS            Export             cc07 
INS            How To   
INS            Notes   



If I use the following:
C#
tn.Nodes.Add(dr["Child"].ToString());


I get an output that looks something like
ASV (root)
g-1
SV
Script
Export
How To
Notes


C#
treeView1.BeginUpdate();
                
foreach(DataRow r in parent.Rows)
{ 	
   tn = treeView1.Nodes.Add(r["ID"].ToString(), r["Parent"].ToString());
                	
   foreach(DataRow dr in child.Rows)
   {
     treeView1.Nodes[treeView1.Nodes.IndexOf(tn.Parent)].Nodes.Add
     (dr["Child"].ToString());
   }
                	
}
treeView1.EndUpdate();
Posted
Updated 16-Jan-12 5:47am
v5
Comments
Anuja Pawar Indore 16-Jan-12 1:37am    
Added pre tag

1 solution

The first question that comes to mind is: have you searched CP and StackOverFlow and the MS docs for how to data bind to a TreeView ?

Second: Are you dealing with a case here where: the possible "depth of nesting" in the TreeView is #2 or #3 ?

My impression is that you describe a case where the TreeView itself has #n Root-level Nodes, each of which corresponds to a Row in the parent.Rows of your DataBase. The "Parent" of a Root Node is always "null."

So, when you mention "grandchild" I'm not sure if you have a potential node depth of #2 or #3.

Your code shows only one level of inner parsing of child nodes, giving you a maximum depth of #2, unless you are thinking that a "root node" does, in a sense, have a "virtual depth" of #1, in spite of its parent being "null."

The other possibility I can't evaluate here is whether you have arbitrary depths of nesting possible: clearly you say some nodes can have children, but others may not, but can there be nodes that have many levels of nesting, beyond #2, or #3. ?

In the absence of a successful data binding strategy: which, imho, is the solution of choice, you are going to need to write recursive code to populate the TreeView, if you do, indeed, intend to have Nodes nested at arbitrarily deep levels.

In your sample code there are several strange aspects to me:

1. where do 'child.rows come from: shouldn't they be derived from parent.rows each time through the outer loop ?

2. you are adding a root-level node: 'tn

a. but then, instead of adding the child nodes to 'tn, you adding them to the calculated parent of 'tn: if 'tn is a root node: its parent is 'null, and there's an error right there.

A little more clarity from you on what your DB structure is, and your exact intent, and I hope I can be more helpful. What exactly do 'parent.rows' and 'child.rows' refer to ?

Meanwhile, consider (as a "wild guess"):
C#
treeView1.BeginUpdate();

foreach(DataRow r in parent.Rows)
{
   tn = treeView1.Nodes.Add(r["ID"].ToString(), r["Parent"].ToString());

   foreach(DataRow dr in ?) // replace '?' with something that retruns child DataRows of 'r !
   {
      tn.Nodes.Add(dr["Child"].ToString());
   }

}
treeView1.EndUpdate();
 
Share this answer
 
v3

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