Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I want to bind data from datatable in treeview control in Asp.net C#. I have one table with columns Id and Name. And I want to create treeview with parent, child and subchild,
For example parents are 0,1,2....9 , childs of 0 are 00,01,02,03, , childs of 1 are 10,11,12,12.....Subchild of 00 is 000,001,002...... for 10 are 101, 102......
How to do that with out parentID column?

Please help...
Posted
Updated 17-Jul-14 23:32pm
v2

Hello,
I think you should work the id column as a varchar variable and cross it in a for loop, taking the length of each id as the hierarchical level of the node, ie length = 1 would root node, the length = 2 the child node and length = 3 subchild node. Possibly you must use recursive functions.
 
Share this answer
 
Hi you can use like this.


Suppose If you have two columns (ParentID and ChildID), You can filter Child rows and bind it to the new treenodes.



C#
private void AddNode(DataTable dt, string ChildID, TreeNode childnode)
     {

         DataRow[] dr = dt.Select("ParentID ='" + ChildID+ "');
         foreach (DataRow drChild in dr)
         {
             TreeNode childnode1 = new TreeNode();
             childnode1.Text = drChild["Name"];
             childnode.Nodes.Add(childnode1);
             AddNode(dtChild, drChild["ChildID"].ToString(), childnode1);
       }
     }



-Thanks
Sharath
 
Share this answer
 
Comments
R.Elena 18-Jul-14 8:12am    
I Have only one column ID for parent and children and column Name, and from that two columns to bind a treeview with parent, child and subchild :/

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