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

GRIDVIEW ZOOM IMAGE

When you have images in your  GridView , you would most likely show them as thumbnails so as to not distort the whole layout. However user would want to look at the full image by clicking on the image or just hovering his mouse over it. In today’s applications, this is a basic requirement and there are just so many third party controls or plugins which would support this functionality. I am going do this conventional way using  javascript  way in this article.  On top of it, I am also going to explain how to get images from database using  HttpHandlers . Example: I am using  Adventure Works  as datasource. We fetch handful of products and bind them to the grid. When page is initially loaded, we retrieve products from Production . Product  table and bind them to the grid. We display some product attributes such as Product ID, Product Number, Product Name, List Price and product’s thumbnail. When user hover his mouse on the page, we fetch the f...

Dynamic Generation of Word Document Report in ASP.NET with HTML

Overview Ever needed to generate Word documents from your ASP.NET applications? There are a couple of components which will help to generate Word documents, but using these components may have some overhead such as Component Registration, setting permissions, licenses, etc., Sometimes, it may even become difficult to understand their features and use them. Generating word document in ASP.NET with HTML is incredibly easy. The following sample demonstrates how to place Heading and Table dynamically in the word document in ASP.NET web applications using HTML. Requirements Microsoft Visual Web Developer 2005 Express Edition Create the Web Application project Add the new Web Site Application project (with name as Generate Word Document) and place the button (with name as btnGenerateDocument) in the Default.aspx web form as shown below. Double click the highlighted code behind file from the Solution Explorer and add the following namespaces. Listing 1 using  System...

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