Binding TreeView control with DataBase in ASP.NET.


In this article we will see how to use the TreeView control . How can we bind data to TreeView manually and how to bind TreeView through database tables. We will see both the scenarios with examples.

We just have to take a treeview control in the form.

Binding Treeview control Manually.



public DataTable RootNodes()
{
DataTable dt = new DataTable("Ward");
dt.Columns.Add("WardId", typeof(int));
dt.Columns.Add("WardNum", typeof(String));
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "12";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "2";
dr1[1] = "13";
dt.Rows.Add(dr1);
return dt;
}

public DataTable ChildNodes()
{
DataTable dt = new DataTable("Bed");
dt.Columns.Add("BedId", typeof(int));
dt.Columns.Add("BedNum", typeof(string));
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "1";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "2";
dr1[1] = "2";
dt.Rows.Add(dr1);
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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);
DataTable dt1 = ChildNodes();
for (int j = 0; j < dt1.Rows.Count; j++)
{
TreeNode childnode = new TreeNode();
childnode.Text = dt1.Rows[j][1].ToString();
firstchild.ChildNodes.Add(childnode);
}
}
}
}


Now we will see how to bind treeview from the database table



public static SqlConnection con;
public static SqlCommand cmd;
string c = ConfigurationManager.ConnectionStrings["HospitalConnectionString"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(c);
cmd = new SqlCommand();
if (!IsPostBack)
{
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);
DataTable dt1 = ChildNodes(Convert.ToInt32(firstchild.Text));
//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);
}
}
}
}
private DataTable RootNodes()
{
string connection = ConfigurationManager.ConnectionStrings["HospitalConnectionString"].ToString();
SqlConnection con = new SqlConnection(connection);
con.Open();
string query = "select Ward_Id,Ward_Num from WardTable";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private DataTable ChildNodes(int wardnum)
{
string connection = ConfigurationManager.ConnectionStrings["HospitalConnectionString"].ToString();
SqlConnection con = new SqlConnection(connection);
con.Open();
string query = "select Bed_Id,Bed_Num from BedTable where Ward_Num = " + wardnum +"";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}


Below is the table structure of ward and bed table:

CREATE TABLE [dbo].[BedTable](
[Ward_Num] [int] NULL,
[Bed_Num] [int] NULL,
[Bed_Status] [int] NULL,
[Bed_Id] [int] IDENTITY(1,1) NOT NULL)

CREATE TABLE [dbo].[WardTable](
[Ward_Id] [int] IDENTITY(1,1) NOT NULL,
[Ward_Num] [int] NULL,
[Max_Bed] [int] NULL)


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: