TREEVIEW IN ASP.NET

Bind Treeview From DataBase in Asp.net

DOWNLOAD SOURCE HERE

Introduction:

In this article we will see how to bind Asp.net Treeview control from database on the parent child relation.

Background:

For this task in my beginner level of programming I’d spend one day. My requirement was that bind treeview control from database value from two different tables which are releated with each other. Finally I got my solution at the end of the day.

In this task it was like bind mainroot node of treeview from parent table and on the basis of value refering to child table get that values and bind it under mainroot nodes. How to do we will see step by step.

Step 1:

Create a web application keep one treeview control on Default.aspx.

Step 2:

Now in Default.aspx.cs page first get the values from Parent table in Datatable or Dataset object as bellow.

//this values can be from Database. For understanding I’m binding it onfly.

public DataTable RootNodes()

{

DataTable dt = new DataTable(“Root”);

dt.Columns.Add(“RootId”, typeof(int));

dt.Columns.Add(“RootDesc”, typeof(String));

DataRow dr = dt.NewRow();

dr[0] = “10”;

dr[1] = “Root Node One”;

dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();

dr1[0] = “20”;

dr1[1] = “Root Node Two”;

dt.Rows.Add(dr1);

return dt;

}

Step 3:

Bind this RootNode values to treeview first.

DataTable dt = RootNodes();

TreeNode headnode = new TreeNode();

headnode.Text = “This Is Head Node”;

TreeView1.Nodes.Add(headnode);

for (int i = 0; i < dt.Rows.Count; i++)

{

TreeNode firstchild = new TreeNode();

firstchild.Text = dt.Rows[i][1].ToString();

headnode.ChildNodes.Add(firstchild);

}

Step 4:

Now get the values for Child Nodes on the basis of Parent nodes value.

//you can pass argument for this method and select the values from database.

public DataTable ChildNodes()

{

DataTable dt = new DataTable(“Child”);

dt.Columns.Add(“ChildId”, typeof(int));

dt.Columns.Add(“ChildDesc”, typeof(string));

DataRow dr = dt.NewRow();

dr[0] = “10”;

dr[1] = “Child Node One”;

dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();

dr1[0] = “20”;

dr1[1] = “Child Node Two”;

dt.Rows.Add(dr1);

return dt;

}

Step 5:

Now bind ChildNodes to Treeview by getting values.

if (!IsPostBack)

{

//Can call From Database also

DataTable dt = RootNodes();

TreeNode headnode = new TreeNode();

headnode.Text = “This Is Head Node”;

TreeView1.Nodes.Add(headnode);

//Loop for binding Parent Values

for (int i = 0; i < dt.Rows.Count; i++)

{

TreeNode firstchild = new TreeNode();

firstchild.Text = dt.Rows[i][1].ToString();

headnode.ChildNodes.Add(firstchild);

//Can call from Database on the basis of value of firstchild

//Here you can pass argument.

DataTable dt1 = ChildNodes();

//Loop for binding Child Values.

for (int j = 0; j < dt1.Rows.Count; j++)

{

TreeNode childnode = new TreeNode();

childnode.Text = dt1.Rows[j][1].ToString();

firstchild.ChildNodes.Add(childnode);

//more over can expand the list.

}

}

}

Conclusion:

In the simple manner we can bind values to treeview control from multiple tables.

Advertisements

5 responses to “TREEVIEW IN ASP.NET

  1. how to display the values from database to treeviw control in asp.net .plse send the code for that
    thanks and regard
    basheed

    • TreeviewNode Parent=New TreeviewNode();
      TreeeviewNode Child=New TreeviewNode()
      TreeviewNode GrandChilde=New TreeviewNode();
      Child.Node.Add(GrandChilde);
      Parent.Node.Add(Child);
      do like this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s