Click here to Skip to main content
Click here to Skip to main content

Binding the ASP.NET TreeView to a DataSet or an ObjectDataSource

By , 1 May 2008
 

Introduction

The TreeView in ASP.NET is a powerful control that helps display hierarchical data. However, unlike other controls, it does not support binding to a DataSet or an ObjectDataSource. I have seen a lot of developers do this the old fashioned way, filling the tree programmatically, which is a waste of time and energy.

The solution

The key to this solution is that the TreeView can bind to any object implementing the interface IHierarchicalDataSource. So, this article presents to you a small class that will take a DataSet as an input and return an object that implements IHierarchicalDataSource so that the TreeView can easily bind with your DataSets.

Under the hood

The class HierarchicalDataSet presents data in a hierarchy, which means supports Parent-Child relationships, just like the nature of a TreeView control. You have nodes, and under some nodes, you have children. Creating this structure in a database involves having a table reference itself to implement the parent-child relationship. Here is how such a table would look like:

Table

Here is a quick example for some records to see how they will present the child parent relationship:

DataRow row = dataSet.Tables[0].NewRow();
row["ID"] = 1;
row["Text"] = "Parent 1";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 2;
row["Text"] = "Parent 2";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 3;
row["ParentID"] = 1;
row["Text"] = "Child 1";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 4;
row["ParentID"] = 1;
row["Text"] = "Child 2";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 5;
row["ParentID"] = 2;
row["Text"] = "Child 3";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 6;
row["ParentID"] = 2;
row["Text"] = "Child 4";
dataSet.Tables[0].Rows.Add(row);

Using the code

Using the code is very simple. You need to call the constructor of the class that takes the DataSet and the two column names needed. The primary key and the foreign key reference the same table.

TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");

The original blog of this article can be found here.

Hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ralph Varjabedian
Chief Technology Officer Xplorium
Lebanon Lebanon
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNot so nice though!memberBalamurali Balaji2-May-08 22:51 
Treeview control supports XmlDataSource; a dataset may be stored as a xml file and can be bound to the treeview control.
 
You don't even write this much of code to build a dataset programmatically and then do the binding.
 
The article is not even good for novice developers.
 
balamurali balaji
MVP (ASP.NET, Visual C# & Device Application Development)
homepage: http://h1.ripway.com/bbmurali_2000

GeneralRe: Not so nice though! PinmemberBalamurali Balaji4-May-08 3:41 
Yeah.. it is supposed to work the way you have mentioned.
 
I have explored your sample thoroughly and I could see only one table data in your dataset. If its a master-detail(parent-child) relationship stored in the dataset, it is supposed to have two tables in the dataset.
 
This means that you represent data in one table in hierarchical fashion. Within a table, you have assumed two columns as parent and child columns. But, not, displaying data from two tables related to each other by common column. If this is the case, where comes the parent-child relationship?
 
My understanding is that you are displaying data from parent and child table. I am still worried about this aspect.
 
balamurali balaji
MVP (ASP.NET, Visual C# & Device Application Development)
homepage: http://h1.ripway.com/bbmurali_2000

GeneralRe: Not so nice though! PinmemberRalph Varjabedian4-May-08 5:05 
Yes, you only have one table and not two tables. You do not need two tables to have master-detail relationship you only need one. And often in these cases it is one since you are displaying the same type of data (same table with circular reference) and not two tables because they have exactly the same columns, I am surprized you are not familier with this database "modeling method". And if you do have two, then a simple inner join can make them into one, covering both cases.
 
The parent-child relationship comes from the circular reference (foriegn key to the table itself), please see the table example model. This is a typical method of creating parent-child relationship and model it in databases, it is a well known method.
 
Well as I said, if you just have one table, with circular reference then your data is done, if you have two tables and inner join will easily make the data into one dataset. Read the first comment on my blog on this topic, someone is explaning a live database table that they used my method for.
 
Ralph Varjabedian
Chief Software specialist

My Blog
Bytesurge.com



GeneralRe: Not so nice though! Pinmembernutwiss14-May-08 5:23 
Agreed. It is entirely common practice to present arbitrarily deep hierarchies as single, self-referencial tables.
 
This is problem being overcome by this solution.
 
Baramurali's master-child relationship, as he correctly points out would be stored in two seperate tables and could, as he points out, be represented and passed to a treeview via XML.
 
However, this is not the problem presented, nor that solved. The problem presented is that of self-referencial, hierarchical data, presented in a flat table structure, not that of *intrinsically* hierarchical XML data, which, let's be honest, is a doddle to data-bind.
 
This is good solution to a tricky problem.
GeneralRe: Not so nice though! Pinmemberrexahs7-Oct-08 6:38 
I have to agree with nutwiss. This is an entirely normal situation and this is a good solution to the problem. I think a slight extension (overload) would be to refactor to allow a DataTable to be passed and not to select just table[0] but this is a matter of minute detail in the scheme of things. Great share which is much appreciated Big Grin | :-D . Why MS did not do this within the framework is a puzzle, but they probably have other things to do...
GeneralRe: Not so nice though! PinmemberRalph Varjabedian9-Oct-08 23:08 
Hi, Thank you for your comment. I am preparing an update to the code soon, there are several points that the readers have noted that will be included in this update. You can check my blog on more readers' comments on this.
 
Ralph Varjabedian
Chief Software specialist

My Blog
Bytesurge.com



General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 2 May 2008
Article Copyright 2008 by Ralph Varjabedian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid