Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a hierarchical structure, i want retrrive the treeview this structure in using

__ID__  | __NAME__  | __PARENT__|
 1      | Patrick   | primary
 2      | Mark      | primary
 3      | Scott     | Mark
 4      | Jason     | Patrick
 5      | Julian    | Primary
 6      | John      | Scott
 7      | Steve     | Scott
 8      | George    | Patrick
 9      | Robert    | George
 10     | Rodney    | George

[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Updated 25-Nov-13 23:53pm
OriginalGriff 26-Nov-13 5:53am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
OriginalGriff 26-Nov-13 5:53am    
And what have you done so far?
If you are waiting for permission, consider it given...
Vinodh.B 26-Nov-13 5:54am    
Don't create duplicate questions. You had already posted this question earlier

See in VB
Private Sub BindTreeViewControl()
		Dim dt As New DataTable() 
		'Assign your database value to dt table  where dt is null now
		Dim dtParent As DataTable = (From f In dt.AsEnumerable() Where f.Field(Of String)("PARENT").ToLower() = "primary"f).ToList().CopyToDataTable()
		For i As Integer = 0 To dtParent.Rows.Count - 1
			Dim root As New TreeNode(dtParent.Rows(i)("NAME").ToString(), dtParent.Rows(i)("ID").ToString())
			root.SelectAction = TreeNodeSelectAction.Expand
			CreateNode(root, dt)
			'Sending table
	Catch Ex As Exception
		Throw Ex
	End Try
End Sub

Public Sub CreateNode(node As TreeNode, Dt As DataTable)
	Dim dtChild As New DataTable()
	Dim varChiled = (From f In Dt.AsEnumerable() Where f.Field(Of String)("PARENT").ToLower() = node.Text.ToLower()f).ToList()
	If varChiled.Count > 0 Then
		dtChild = varChiled.CopyToDataTable()
	End If
	If dtChild.Rows.Count = 0 Then
	End If
	For i As Integer = 0 To dtChild.Rows.Count - 1
		Dim Childnode As New TreeNode(dtChild.Rows(i)("NAME").ToString(), dtChild.Rows(i)("ID").ToString())
		Childnode.SelectAction = TreeNodeSelectAction.Expand
		CreateNode(Childnode, Dt)
End Sub
Share this answer
saifudheen kt 26-Nov-13 11:20am    
Share this answer
See !
did the code for it

private void BindTreeViewControl()
           DataTable dt = new DataTable();
           dt.Columns.Add("ID", typeof(int));
           dt.Columns.Add("NAME", typeof(string));
           dt.Columns.Add("PARENT", typeof(string));
           dt.Rows.Add(1, "Patrick ", "primary");
           dt.Rows.Add(2, "Mark", "primary");
           dt.Rows.Add(3, "Scott", "Mark");
           dt.Rows.Add(4, "Jason", "Patrick");
           dt.Rows.Add(5, "Julian", "Primary");
           dt.Rows.Add(6, "John", "Scott");
           dt.Rows.Add(7, " Steve", "Scott");
           dt.Rows.Add(8, " George", "Patrick");
           dt.Rows.Add(9, "Robert", "George");
           dt.Rows.Add(10, "Rodney", "George");
           // DataRow[] Rows = dt.Select("ParentId IS NULL"); Get all parents nodes
           DataTable dtParent = (from f in dt.AsEnumerable()
                                 where f.Field<string>("PARENT").ToLower() == "primary"
                                 select f).ToList().CopyToDataTable();
           for (int i = 0; i < dtParent.Rows.Count; i++)
               TreeNode root = new TreeNode(dtParent.Rows[i]["NAME"].ToString(), dtParent.Rows[i]["ID"].ToString());
               root.SelectAction = TreeNodeSelectAction.Expand;
               CreateNode(root, dt);//Sending table
       catch (Exception Ex) { throw Ex; }
   public void CreateNode(TreeNode node, DataTable Dt)
       DataTable dtChild = new DataTable();
       var varChiled = (from f in Dt.AsEnumerable()
                             where f.Field<string>("PARENT").ToLower() == node.Text.ToLower()
                             select f).ToList();
       if (varChiled.Count > 0) dtChild = varChiled.CopyToDataTable();
       if (dtChild.Rows.Count == 0) { return; }
       for (int i = 0; i < dtChild.Rows.Count; i++)
           TreeNode Childnode = new TreeNode(dtChild.Rows[i]["NAME"].ToString(), dtChild.Rows[i]["ID"].ToString());
           Childnode.SelectAction = TreeNodeSelectAction.Expand;
           CreateNode(Childnode, Dt);
Share this answer
saifudheen kt 26-Nov-13 7:37am    
this values are sql Table,i am using in

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