Generate tree nodes

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

      

+1


source to share


1 answer


Here is some pseudo code that might help you get 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 all the elements without the parent node id (root nodes) by creating a node for them and then calling the recursive function above.

+3


source







All Articles