tags:

views:

1552

answers:

3

How can I generate treeview nodes programmatically for this collection

node id          parent node id

-------          --------------

100                 null           //this is the root node

101                 100

123                 101

124                 101

126                 101

103                 100

104                 100

109                 100

128                 109

122                 100

127                 122

129                 127

130                 129
A: 

Here's an example in VB.NET which can be translated to C# and adapted to your scenario.

Darin Dimitrov
is there any c# examples?
Sorry for being too frank Sara but it sounds like you want people to get the job done for you. It's not that hard to understand VB.NET code if you know C#.
rodbv
no im not!! i have been working on this problem for 2 days!!im new to .net thats why im kind of stuck!!anyway thats for ur help
+3  A: 

Here is some pseudo-code that might help to get you started

AddChildNodes(TreeNode parentNode)
{
   var childNodeIds GetChildNodeIds(parentNode.Id);
   foreach (int childNodeId in childNodeIds)
   {
      TreeNode childNode = new TreeNode();
      //set other properties...

      //add to parent          
      parentNode.Nodes.Add(childNode);

      //call same function recursively
      AddChildNodes(childNode);
   }

}

Then in your program you get started by getting all items without parent node id (root nodes), creating a node for them and then calling the recursive function above.

rodbv
it didnt work,because i dont only have a parent and child,i hav sub children (root ,child 1,child 2 child 3 child 4)my problem is with the number of children cause im filling my treeview from the data base and this table is an output from a storedprocedure
This algorithm is recursive, it should take care of that as well, no matter how many levels you have. Just use this code and the list you provided and make a simulation on a piece of paper, and you will see it works.
rodbv
ya ya i got that,but some values get lost,im gonna hav to fix sm stuff
If values are being lost you have a bug in the implementation, it doesnt mean the algorithm is wrong. Have you even tried to implement it as I suggested to you?
rodbv
whats up people!!im doing it,i told u im gonna hav to fix sm stuff in my code!!
heyyyy thank u thank u thank u,i solved my problem,your answer was very helpful thx alot
You can accept it as an answer then ;)
rodbv
A: 

What programming language is this to be written in?

thomask
c# would be great