Showing hierarchical data from the database using the tree view control in asp.net 2.0

Hi

One of the cool new controls of the asp.net 2.0 is the tree view control. But there is no direct support in the treeview control to populate hierarchical data on the fly from the datasource. In this post I will talk about populating hierarchical data from the database to be shown in the treeview.

For the Example purpose lets say we have a table with hierarchical data. The table has three fields Cat_id, Name and ParentID. The parentId references the Cat_id to make the data hierarchical. On how to retirieve hierarchical data check my previous post.

First define a treeview control in the page like below

<asp:TreeView 

  ID="TreeView1"

  ExpandDepth="0"

  PopulateNodesFromClient="true"

  ShowLines="true"

  ShowExpandCollapse="true"

  runat="server" />

Here we are setting the initial expand depth of the treeview to 0. we will populate the data from the server as and when required by using the client-side node populating feature. This will make better user experience. Using this method will not make unnecessary postback for each record.

Now to populate the data we will have to write some code. In the p[age load inside the  Not IsPostback block call the RootLevel() Function.  The root level function is something like this

Private Sub RootLevel()

  Dim objConn As New SqlConnection( "connstr")

  Dim objCommand As New SqlCommand("select cat_id, Name,(select count(*) FROM SampleCategories " _

    & "WHERE parentid=sc.Cat_id) childnodecount FROM SampleCategories sc where parentID IS NULL", _

    objConn)

  Dim da As New SqlDataAdapter(objCommand)

  Dim dt As New DataTable()

  da.Fill(dt)

 

  PopulateNodes(dt, TreeView1.Nodes)

End Sub

Private Sub PopulateNodes(ByVal dt As DataTable, _

  ByVal nodes As TreeNodeCollection)

  For Each dr As DataRow In dt.Rows

    Dim tn As New TreeNode()

    tn.Text = dr("Name").ToString()

    tn.Value = dr("id").ToString()

    nodes.Add(tn)

 

    ' enable on-demand populating if node has child records

    tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)

  Next

End Sub

Private Sub PopulateSubLevel(ByVal parentid As Integer, _

  ByVal parentNode As TreeNode)

  Dim objConn As New SqlConnection("connstr")

  Dim objCommand As New SqlCommand("select Cat_id, Name,(select count(*) FROM SampleCategories " _

    & "WHERE parentid=sc.Cat_id) childnodecount FROM SampleCategories sc where parentID=@parentID", _

    objConn)

  objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid

 

  Dim da As New SqlDataAdapter(objCommand)

  Dim dt As New DataTable()

  da.Fill(dt)

  PopulateNodes(dt, parentNode.ChildNodes)

End Sub

‘This event is called when the user clicks to get additional data.

Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _

  ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate

  PopulateSubLevel(CInt(e.Node.Value), e.Node)

End Sub

TreeNodePopulate is raised for a TreeNode which is expanded by the user for the first time. Due to PopulateNodesFromClient (TreeView) and PopulateOnDemand (TreeNode) settings, this happens with the client-side callback mechanism which is handled by the ASP.NET Page framework.

The PopulateNodes method iterates through all rows in the DataTable, and if the given node has child nodes, it sets the PopulateOnDemand property of the TreeNode object according to that. This has the effect that the expand/collapse icon is shown if the node has child nodes.

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 4/18/2007 2:23:27 AM

hi vikram this is navneet i hv jst seen ur provided code for tree view it helps me a lot bt i want to confirm from ur side is it possible to use tree view if i want to show a list of books available frm a table 'd when user click on specific book he vl get d details of that specific book...m wating fr ur reply...thnx

Posted on 6/19/2007 8:48:42 AM

I can populate the treeview from a database, but can't find anyway that tells me how to fire a new treeview from a dropdownlistbox.

I have a load of categories in a dropdown listbox, i then want to display the results in a treeview.

Can this be done.??

Posted on 10/20/2007 10:13:28 AM

Would you be interested in reviewing the GridViewTree? It is based on the GridView so it has all of the same binding options. It can also retrieve and bind each node's child nodes as they are expanded.

http://www.digitaltools.com/GVT.aspx
Thanks, Jim

Posted on 11/28/2007 7:05:01 PM

plz suggest me how to display database records in a tree view as such MLM (Chain) Business. I am using ASP and not ASP.NET.

help me.

Regards
Santosh

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia