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

Scrollable GridView with Fixed Headers using jQuery Plugin

Using the same example I have created a jQuery Plugin for Scrollable GridView with Fixed header so that you can directly make a GridView scrollable.   HTML Markup < form   id ="form1"   runat ="server"> < asp : GridView   ID ="GridView1"   runat ="server"   AutoGenerateColumns   =   "false"> < Columns > < asp : BoundField   DataField   =   "ContactName"   HeaderText   =   "Contact Name"   /> < asp : BoundField   DataField   =   "City"   HeaderText   =   "City"   /> < asp : BoundField   DataField   =   "Country"   HeaderText   =   "Country"   /> Columns > asp : GridView > form >   Applying the Scrollable Grid jQuery Plugin < script   src ="Scripts/jquery-1.4.1.min.js"   type ="text/javascript"> script > < script   src ="Scripts/Scro...

JQuery lightbox image slideshow gallery in asp.net

Introduction: In this article I will explain how to create lightbox image slideshow in asp.net using JQuery. Description:  In previous post I explained about  Ajax SlideshowExtender sample  to display images slideshow. Now in this article I will explain how to create lightbox image slideshow in asp.net using JQuery. In many websites generally we will see different types of slideshows like whenever we click on image that is automatically open with lightbox effect and we have a chance to see all the remaining images by press next or previous options in slideshow. All these Slideshows are implemented by using JQuery plugins. After seen those slideshows I decided to write post to use JQuery plugins to implement beautiful slideshown in asp.net.  To implement this one I am using  previous post  insert and display images from folder based on imagepath in database  because in that post I explained clearly how to insert images into our project folder a...

GRIDVIEW GROUPING

When displaying data, we sometimes would like to group data for better user experience or when displaying long list of hierarchal data, we would want to display them in a tree view kind of structure. There is more than way of doing this, but I am going to explain achieving this functionality using  AJAX Collapsible Panel Extender Control . Overview: I am going to use  Adventure Works  as datasource. Every product in  Production.Product  table belongs to a product sub category. We fetch handful of products and the sub categories they belong to from the database. Our objective is to list all the available sub categories and allow user to  expand/collapse  to look/hide the list of products belonging to each subcategory. Database Connection Added following entry under  connectionStrings  element in  web.config . < add   name = "Sql"   connectionString="Data Source=(local); Initial  Catalog = AdventureWorks ...