Wednesday, 16 January 2013

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.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.