A TreeView control is a useful control in ASP.NET.

Introduction

Create TreeView in ASP.NET C# with SQL Database.

Step 1: Database

First create a table in the database as in the following.

Table 1: State

  1. create table IN_State
  2. (
  3. S_Id int identity(1,1) primary key,
  4. S_Name varchar(30)
  5. )
Table 2: City
  1. create table IN_City
  2. (
  3. C_Id int identity(1,1) primary key,
  4. C_Name varchar(30),
  5. S_Id int foreign key references IN_State(S_Id)
  6. )
Step 2: Visual Studio
  1. Go to the project the Solution Explorer.
  2. Add a new item as in the following:

    Add new Item
    Figure 1: Add new Item

  3. Add a Web Form.

    Add Web From
    Figure 2: Add Web From

Step 3: From UI Side

Now add a TreeView control on the .aspx page as in the following UI code:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="UI_TreeView" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <asp:TreeView runat="server" ID="tview">
  6. </asp:TreeView>
  7. </asp:Content>
Step 4: From UI Code Side

Now right-click on the page design and go to View Code and click it.

Viewcode
Figure 3: Viewcode

Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. public partial class UI_TreeView : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!Page.IsPostBack)
  15. {
  16. string connection = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
  17. using (SqlConnection Conn = new SqlConnection(connection))
  18. {
  19. string State = "Select * from IN_State";
  20. string City = "Select * from IN_City";
  21. string Treeview = State + ";" + City;
  22. DataSet ds = new DataSet();
  23. SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
  24. da.Fill(ds);
  25. ds.Tables[0].TableName = "IN_State";
  26. ds.Tables[1].TableName = "IN_City";
  27. DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);
  28. ds.Relations.Add(dr);
  29. foreach (DataRow drState in ds.Tables["IN_State"].Rows)
  30. {
  31. TreeNode NDState = new TreeNode();
  32. NDState.Text = drState["S_Name"].ToString();
  33. NDState.Value = drState["S_Id"].ToString();
  34. tview.Nodes.Add(NDState);
  35. foreach (DataRow drCity in drState.GetChildRows("StateCity"))
  36. {
  37. TreeNode NDCity = new TreeNode();
  38. NDCity.Text = drCity["C_Name"].ToString();
  39. NDCity.Value = drCity["C_Id"].ToString();
  40. NDState.ChildNodes.Add(NDCity);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
Note: Please maintain a database connection string in your project.

Now the TreeView control code is complete. Run the design page and display the created TreeView as in the following.

Treeview
Figure 4: Treeview

I hope you have understood how to create a TreeView with a SQL table.