Skip to content

Categories:

Binding a Hyrarchial Dataset to an ASP.net Treeview control

Recently I was doing some practice in asp.net data access layer. It is great technique to access your database objects.

I was trying to bind my hyrarchial database table (recursive table) to an asp.net treeview control and I wanted something very simple. Interestingly I couldn’t find any direct databinding methods for a treeview to the recursive table. Atleast there’s no simple method to make the binding with the treeview (correct me if I am wrong) unlike other controls like datagrid, datalist etc.Since ASP.net Treeview understands only hyrarchial dataset, I have to find a way to convert my dataset to a hyrarchial dataset.

On further dig, I found a nice article by Ralph Varjabedian. He wrote an ASP.net class to convert the dataset to a hyrarchial dataset. Thanks to Ralph!

And finally I was successfull to bind the control with the dataset in the way I wanted.

Here is the Code I wrote

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataSet1TableAdapters;

public partial class Campaigns : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{   DataSet dataSet = new DataSet(); //Dataset
TableAdapter CampaignTableAdapter = new TB_campaignTableAdapter(); // data table adapter
dataSet.Tables.Add(CampaignTableAdapter.GetData()); // populating the dataset
tvCampaign.DataSource = new HierarchicalDataSet(dataSet, "ID", "id_parent", 0); // converting to hyrarchial dataset
tvCampaign.DataBind();
tvCampaign.CollapseAll();            // Collapsing all nodes by default
}
}

And I am interested to learn if there’s any other better solution for this.

Posted in Web development.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.