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        {     ...

Hierarchical GridView in ASP.NET with AJAX, JQuery implementation - Part 1

Previously I had blogged some post related to showing two grids in hierarchical way. For example - when I need to show an invoice reports on the screen, the user requesting to show the list of invoices at initial time and they can drill down the details of item details of each invoice by clicking on respective row. The implementations can be in multiple ways, but I had blogged the implementation with AJAX and without AJAX using Java script on ASP.NET page. Below are the URLs of the same – Hierarchical GridView in ASP.NET Hierarchical GridView in ASP.NET with AJAX, Javascript implementation In this post I am taking the same requirements done before and enhance with some additional functionality as per readers expectations. The requirement on this implementation will be – The page should show a Grid View with list of Orders with Expand icon on the first column. On click of expand icon on each row, the Order Details of the Order must be fetched from the database ...

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 ...