Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
it's coming from a table

l1 l2 l3
b bb bbh
b bb bbc
b bc bba

i want the treeview to be something like this

b
---bb
------bbh
------bbc
---bc
------bba

What I have tried:

I am new to c# kindly help and please let me know an easy way to implement it.
Posted
Updated 31-Jul-17 0:47am
Comments
BillWoodruff 31-Jul-17 8:26am    
Is this WinForm ? The complexity of the solution will depend on the extent to which the DataTable has a regular, invariant, structure.
Graeme_Grant 31-Jul-17 8:28am    
Your guess is as good as mine
BillWoodruff 31-Jul-17 9:24am    
I'm clear that I won't write code unless the context is clear.

"I do not enlighten those who are not eager to learn,
nor arouse those who are not anxious
to give an explanation themselves.
If I have presented one corner of the square
and they cannot come back to me with the other three,
I should not go over the points again."

From the "Analects" by Kong Fuzi (Confucius), approximately 2,500 years ago

:)
Graeme_Grant 31-Jul-17 10:00am    
TreeViews are pretty much all the same ... so it took no effort ... but I do understand where you are coming from.
BillWoodruff 31-Jul-17 22:07pm    
"TreeViews are pretty much all the same ..." I disagree: the WinForm TreeView has no equivalent to many WebUI features like 'PopulateOnDemand.'

1 solution

The TreeView control is a hierarchical data control. Child nodes need a reference to a parent node. So you should have a table that has the fields: Id & parentId. The child <> parent relationship is then parentId = Id

So once you load your data into a DataTable, you can do something like this:
C#
foreach (DataRow row in data.Rows)
{
    TreeNode node = new TreeNode(dr["field1"], dr["fieldId"])
    node.PopulateOnDemand = true;

     TreeView1.Nodes.Add(node);
 }
 
 protected void PopulateNode(Object sender, TreeNodeEventArgs e)
 {
     string fieldId = e.Node.Value;
     //select from table where parentId = fieldId.
     foreach (DataRow row in topics.Rows)
     {
         TreeNode node = new TreeNode(dr["field1"], dr["fieldId"])
         node.PopulateOnDemand = true;

         e.Node.ChildNodes.Add(node);
     }

 }
 
Share this answer
 
v3
Comments
BillWoodruff 31-Jul-17 8:26am    
Do you see any sign that the OP is using System.Web.UI.WebControls here for the TreeView ... not WinForms ?

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