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 WWW.Codedisplay.com



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:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Hierarchical_TreeView.aspx.cs" Inherits="Hierarchical_TreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TreeView to display hierarchical or group break down data</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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">
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

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);
                da.Fill(dsFullData);
                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"]);
                dsFullData.Relations.Add(Department_Subject);

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

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

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

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


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.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • 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