Skip to main content

TREEVIEW hierarchical ASP.NET



TreView control to display hierarchical data or group break down data or relational data in Asp.Net

In most of the cases developers need to display Hierarchical data or group breakdown using TreeView control in Asp.Net. Most of the ways you can follow to display such type of group data or relational data or hierarchical data such as gridview, repeater control but if something related to navigational issues then I think TreeView control is the perfect control to display Hierarchical or group or relational data. To do this example please create the below three tables with sample data in your database first to run my example code:

Dept_Sub_Techer

Now add an aspx page in your project.
Modify the HTML Matkup code by below code:
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Hierarchical_TreeView.aspx.cs" Inherits="Hierarchical_TreeView" %>
02   
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04   
05 <html xmlns="http://www.w3.org/1999/xhtml" >
06 <head runat="server">
07     <title>TreeView to display hierarchical or group break down data</title>
08 </head>
09 <body>
10     <form id="form1" runat="server">
11     <div>
12         <asp:Label runat="server" ID="lbl" Font-Bold="true">TreeView to display hierarchical data</asp:Label>
13         <hr />
14         <asp:TreeView ID="TreeView1" runat="server" ShowLines="true">
15         </asp:TreeView>
16     </div>
17     </form>
18 </body>
19 </html>

Now go to the page_load event and write the below code:
01 using System;
02 using System.Data;
03 using System.Web.UI.WebControls;
04 using System.Data.SqlClient;
05 using System.Web.Configuration;
06   
07 public partial class Hierarchical_TreeView : System.Web.UI.Page
08 {
09     protected void Page_Load(object sender, EventArgs e)
10     {
11         if (!Page.IsPostBack)
12         {
13             string ConnString = WebConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
14             using (SqlConnection Conn = new SqlConnection(ConnString))
15             {
16                 string SSQLDepartment = "Select * from Department";
17                 string SSQLSubject = "Select * from Subject";
18                 string SSQLTeacher = "Select * from Teacher";
19                 string SFullSQL = SSQLDepartment + ";" + SSQLSubject + ";" + SSQLTeacher;             
20   
21                 DataSet dsFullData = new DataSet();
22                 SqlDataAdapter da = new SqlDataAdapter(SFullSQL, Conn);
23                 da.Fill(dsFullData);
24                 dsFullData.Tables[0].TableName = "Department";
25                 dsFullData.Tables[1].TableName = "Subject";
26                 dsFullData.Tables[2].TableName = "Teacher";
27   
28                 DataRelation Department_Subject = new DataRelation("DeptSub", dsFullData.Tables["Department"].Columns["ID"], dsFullData.Tables["Subject"].Columns["DeptID"]);
29                 dsFullData.Relations.Add(Department_Subject);
30   
31                 DataRelation Subject_Teacher = new DataRelation("SubTech", dsFullData.Tables["Subject"].Columns["ID"], dsFullData.Tables["Teacher"].Columns["SubID"]);
32                 dsFullData.Relations.Add(Subject_Teacher);
33   
34                 foreach (DataRow oDepartment in dsFullData.Tables["Department"].Rows)
35                 {
36                     TreeNode NodeDepartment = new TreeNode();
37                     NodeDepartment.Text = oDepartment["Name"].ToString();
38                     NodeDepartment.Value = oDepartment["ID"].ToString();
39                     TreeView1.Nodes.Add(NodeDepartment);
40   
41                     foreach (DataRow oSubject in oDepartment.GetChildRows("DeptSub"))
42                     {
43                         TreeNode NodeSubject = new TreeNode();
44                         NodeSubject.Text = oSubject["Name"].ToString();
45                         NodeSubject.Value = oSubject["ID"].ToString();
46                         NodeDepartment.ChildNodes.Add(NodeSubject);
47   
48                         foreach (DataRow oTeacher in oSubject.GetChildRows("SubTech"))
49                         {
50                             TreeNode NodeTeacher = new TreeNode();
51                             NodeTeacher.Text = oTeacher["Name"].ToString();
52                             NodeTeacher.Value = oTeacher["ID"].ToString();
53                             NodeSubject.ChildNodes.Add(NodeTeacher);
54                         }
55                     }
56                 }
57             }
58         }
59     }
60 }

Now run the project and hope you will get below interface:
TreeView

Hope now you can represent hierarchical data or group break down data or relational data using asp.net TreeView control.

Comments

Popular posts from this blog

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble

Nested GridView Example In Asp.Net With Expand Collapse

This example shows how to create Nested GridView In Asp.Net Using C# And VB.NET With Expand Collapse Functionality. I have used JavaScript to Create Expandable Collapsible Effect by displaying Plus Minus image buttons. Customers and Orders Table of Northwind Database are used to populate nested GridViews. Drag and place SqlDataSource from toolbox on aspx page and configure and choose it as datasource from smart tags Go to HTML source of page and add 2 TemplateField in <Columns>, one as first column and one as last column of gridview. Place another grid in last templateField column. Markup of page after adding both templatefields will like as shown below. HTML SOURCE 1: < asp:GridView ID ="gvMaster" runat ="server" 2: AllowPaging ="True" 3: AutoGenerateColumns ="False" 4: DataKeyNames ="CustomerID" 5: DataSour