Sunday, May 9, 2010

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

Please visit my new Web Site

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:


Now add an aspx page in your project.
Modify the HTML Matkup code by below code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Hierarchical_TreeView.aspx.cs" Inherits="Hierarchical_TreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>TreeView to display hierarchical or group break down data</title>
    <form id="form1" runat="server">
        <asp:Label runat="server" ID="lbl" Font-Bold="true">TreeView to display hierarchical data</asp:Label>
        <hr />
        <asp:TreeView ID="TreeView1" runat="server" ShowLines="true">

Now go to the page_load event and write the below code:
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Hierarchical_TreeView : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            string ConnString = WebConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
            using (SqlConnection Conn = new SqlConnection(ConnString))
                string SSQLDepartment = "Select * from Department";
                string SSQLSubject = "Select * from Subject";
                string SSQLTeacher = "Select * from Teacher";
                string SFullSQL = SSQLDepartment + ";" + SSQLSubject + ";" + SSQLTeacher;             

                DataSet dsFullData = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(SFullSQL, Conn);
                dsFullData.Tables[0].TableName = "Department";
                dsFullData.Tables[1].TableName = "Subject";
                dsFullData.Tables[2].TableName = "Teacher";

                DataRelation Department_Subject = new DataRelation("DeptSub", dsFullData.Tables["Department"].Columns["ID"], dsFullData.Tables["Subject"].Columns["DeptID"]);

                DataRelation Subject_Teacher = new DataRelation("SubTech", dsFullData.Tables["Subject"].Columns["ID"], dsFullData.Tables["Teacher"].Columns["SubID"]);

                foreach (DataRow oDepartment in dsFullData.Tables["Department"].Rows)
                    TreeNode NodeDepartment = new TreeNode();
                    NodeDepartment.Text = oDepartment["Name"].ToString();
                    NodeDepartment.Value = oDepartment["ID"].ToString();

                    foreach (DataRow oSubject in oDepartment.GetChildRows("DeptSub"))
                        TreeNode NodeSubject = new TreeNode();
                        NodeSubject.Text = oSubject["Name"].ToString();
                        NodeSubject.Value = oSubject["ID"].ToString();

                        foreach (DataRow oTeacher in oSubject.GetChildRows("SubTech"))
                            TreeNode NodeTeacher = new TreeNode();
                            NodeTeacher.Text = oTeacher["Name"].ToString();
                            NodeTeacher.Value = oTeacher["ID"].ToString();

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

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


Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash