User Id :    Password :      New Member   Forgot Password
 
using treeview to show table data in hierarchical structure
Description In this article I am going to show how we can have table data in a tree view format.   No. of Views     1481
  Rating     0
Author Rahul Saxena   Posted On     20 Oct 2011
Tags ASP.NET,Server Control    

Sample Code   Download Code

 

In this article I am going to show how we can have a table data in a tree view format. Treeview is a viewer for hierarchical structures.
This is my table data.

 

Image 1.

I put my database in App_data folder inside application.

This is my aspx code

 

<div>
    <table cellpadding="2" cellspacing="2" border="2" width="50%" align="center">
        <tr>
            <td>
               Hierarchical Structure of Table Data
            </td>
        </tr>
        <tr>
            <td>
                <asp:TreeView ID="TreeView1" ExpandDepth="0" PopulateNodesFromClient="true" ShowLines="true"
                    ShowExpandCollapse="true" runat="server" />
            </td>
        </tr>
    </table>
</div>

 

This is aspx.cs code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetData();
        }
    }
 
    public void GetData()
    {
        con = new SqlConnection("Data Source=.; Initial Catalog=MyData; uid=sa; pwd=wintellect;");
        cmd.CommandText = "Select * from CourseDetail";
        cmd.Connection = con;
        da = new SqlDataAdapter(cmd);
        con.Open();
        cmd.ExecuteNonQuery();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr.ItemArray[1] == DBNull.Value)
                {
                    TreeNode t = BuildNode((string)dr.ItemArray[2]);
                     t.Value = "0";
                    FillNodeChildren(t, int.Parse(dr.ItemArray[0].ToString()));
                    this.TreeView1.Nodes.Add(t);
                }
            }
        }
    }
 
    private TreeNode BuildNode(string Text)
    {
        return new TreeNode(Text);
    }
 
    // The level of tree 
    int intLevel = 0;
    // Recursive function to populate Treeview node 
    public void FillNodeChildren(TreeNode _Parent, int _ParentID)
    {
        intLevel++;
        DataView Data = new DataView(ds.Tables[0]);       
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if (dr.ItemArray[1] != DBNull.Value)
            {
                if (object.Equals(dr.ItemArray[1].ToString(), _ParentID.ToString()))
                {
                    TreeNode t = BuildNode((string)dr.ItemArray[2]);
					t.Value = intLevel.ToString();
                    FillNodeChildren(t, int.Parse(dr.ItemArray[0].ToString()));                  
                    _Parent.ChildNodes.Add(t);
                    System.Diagnostics.Trace.WriteLine("Added node at level " + intLevel.ToString());
                }
            }
 
        }
        intLevel--;
    } 
}

 

Output

When I run the application then the page will be as follows

 

 

If I expand the nodes then

 

 

Nice one na....

 

 

 

About Author

About Author Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books. Rahul Saxena
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by