Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
NET application where I need to use a TreeView Control. My problem is that I do not want to use parentID column thing to add nodes dynamically to my treeview.

I want to use "Level" Column from datatbase which has level numbers like
SQL
Level    Name
5        Level 1
4        Level 2
3        Level 3
2        Level 4
1        Level 5

So I want to have a tree view which has the highest number as parent node like this
C#
Level1
  Level2
     Level3
       Level4
         Level5

Here is my code:
C#
foreach (DataRow level1DataRow in ds.Tables[0].Rows)
               {
                  // if (!string.IsNullOrEmpty(level1DataRow["Node Level"].ToString()))
                   {
                       TreeNode parentTreeNode = new TreeNode();
                       parentTreeNode.Text = level1DataRow["Level"].ToString() + level1DataRow["Name"].ToString();
                       parentTreeNode.Value = level1DataRow["Level"].ToString();


                       parentTreeNode.SelectAction = TreeNodeSelectAction.SelectExpand;

                       parentTreeNode.Expand();

                       parentTreeNode.Selected = true;

                       this.TreeView1.Nodes.Add(parentTreeNode);


                       GetChildRows(parentTreeNode,parentTreeNode.Value);
                       TreeView1.SelectedNode.Selected = false;

                       db.Close();

other method to get child

C#
foreach (DataRow row in ds.Tables[0].Rows)
           {
               int child_level = Convert.ToInt32(row["Node Level"].ToString());
               if(child_level < nodeLevel && child_level != nodeLevel)
               {
                   TreeNode childTreeNode = new TreeNode();

                   childTreeNode.Text = row["Level"].ToString() + row["Name"].ToString();

                   childTreeNode.Value = row["Level"].ToString();



                   childTreeNode.SelectAction = TreeNodeSelectAction.SelectExpand;


                   parent.ChildNodes.Add(childTreeNode);

                   GetChildRows(childTreeNode, childTreeNode.Value);


               }

           }

Please let me know how to do this?
Posted
Comments
Patrice T 6-Aug-15 17:22pm    
What is your question ?
Member 11842305 6-Aug-15 18:04pm    
I want to display a tree view as I shown above.
Patrice T 6-Aug-15 18:10pm    
I allow you to do it !
Member 11842305 6-Aug-15 18:11pm    
I am sorry sir but i didn't get you.
Patrice T 6-Aug-15 18:17pm    
you want to do something !
I allow you to do it !

WHAT IS YOUR PROBLEM ? WHAT IS YOUR QUESTION ?

1 solution

 
Share this answer
 
Comments
Member 11842305 7-Aug-15 13:40pm    
Hi Sir, Thank you for your reply. I read the article that you have provided but the problem with me is that I want to use just the ID column i do not want to add any other column into my database. So can you please tell me a way, how to do it?

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