Geeks With Blogs

News
Employers
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 MyGooglePageRank.com

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
  
                 101
104             Node1_Node2                   101 
105             Node2_Node1                   102 
106             Node2_Node2                   102
107             Node2_Node1_Node1        102


Function

private
void FillData(TreeViewNode
parentNode)
{
   DataSet ds = new DataSet();SqlConnection cnn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Testing;Integrated Security=True"
);
   SqlDataAdapter
da;
   cnn.Open();
   if (parentNode==null
)
      da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId IS Null"
, cnn);
   else
      da = new SqlDataAdapter("Select [ID], [Data], [ParentId] from Treedata Where ParentId="
+ parentNode.ID , cnn);
   da.Fill(ds);
   Response.Write(ds.Tables[0].Rows.Count);
   foreach (DataRow dataRow in
ds.Tables[0].Rows)
   {
      ComponentArt.Web.UI.TreeViewNode
node;
      node = new ComponentArt.Web.UI.TreeViewNode
();
      node.ID = dataRow["ID"
].ToString();
      node.Text = dataRow["Data"
].ToString();
      if (parentNode==null
)
      {
         TreeView1.Nodes.Add(node);
      }
      else
 
      {
         parentNode.Nodes.Add(node); 
      }

         //Recursive call
         FillData(node);
   }

//Call the function with null argument
FillData(null);

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;
ds.Relations.Add("NodeRelation"
,ds.Tables[0].Columns[primaryKeyColumn]
,ds.Tables[0].Columns[parentIDColumn]);


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: GeeksWithBlogs.net