Geeks With Blogs

Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with

Narendra Tiwari

Following is the table structure, and code for creating and filling the n level heirarchical treeview control using Recursion on your web (aspx) page, In my case I used the Component Art grid control, It can be any heirarchical control with Node property.
Here is an assumption that there is a TreeviewControl On your page.

Table Structure

[ID]             [Data]                              [ParentId]
101             Node1                              Null
102             Node2                              Null
103             Node1_Node1
104             Node1_Node2                   101 
105             Node2_Node1                   102 
106             Node2_Node2                   102
107             Node2_Node1_Node1        102


void FillData(TreeViewNode
   DataSet ds = new DataSet();SqlConnection cnn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Testing;Integrated Security=True"
   if (parentNode==null
      da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId IS Null"
, cnn);
      da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId="
+ parentNode.ID , cnn);
   foreach (DataRow dataRow in
      node = new ComponentArt.Web.UI.TreeViewNode
      node.ID = dataRow["ID"
      node.Text = dataRow["Data"
      if (parentNode==null

         //Recursive call

//Call the function with null argument

I believe this will be the good one for you....enjoy

Posted on Monday, October 23, 2006 7:32 PM .Net | Back to top

Comments on this post: n Level TreeView

# re: n Level TreeView
Requesting Gravatar...
You can do the same thing with just one query total, instead of one query per node, which will perform MUCH better.

Just grab the entire table (the queries you have will work fine, just drop off the where clause)

then add a relationship to the dataset using code like this :

ds.EnforceConstraints = false;

to build the parent child relationships.

Then you can "query" the dataset to find the child rows with this call :

foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))

You still have the same logic as in your function, but you only have one database call, which will be much more scaleable.
Left by Jason Coyne on Oct 23, 2006 8:47 PM

# re: n Level TreeView
Requesting Gravatar...
Please, if possible, grab the code. Thank you very much
Left by mohamad on Nov 11, 2017 4:54 PM

Your comment:
 (will show your gravatar)

Copyright © Narendra Tiwari | Powered by: