Click here to Skip to main content
Licence CPOL
First Posted 1 May 2008
Views 57,049
Downloads 856
Bookmarked 32 times

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

By | 1 May 2008 | Article
The TreeView can not bind to a DataSet or to an ObjectDataSource. With one line of code, you can do that now.

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

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembershrikant.sherekar22:07 3 May '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey0:40 16 Feb '12  
GeneralGreat Article, but what if I have checkboxes in my tree view by having this ShowCheckBoxes="All" Pinmemberrperetz9:19 12 Feb '10  
GeneralHi Ralph Varjabedian PinmemberMagesh Murugesan23:35 10 Jan '10  
GeneralUtterly awesome! Pinmemberdropit3:22 2 Oct '08  
GeneralRe: Utterly awesome! PinmemberRalph Varjabedian22:35 2 Oct '08  
GeneralSlight bug in this PinmemberDMardell2:35 2 Sep '08  
GeneralRe: Slight bug in this PinmemberRalph Varjabedian3:26 2 Sep '08  
GeneralRe: Slight bug in this PinmemberDMardell8:23 2 Sep '08  
GeneralRe: Slight bug in this PinmemberRalph Varjabedian22:37 2 Sep '08  
GeneralRe: Slight bug in this PinmemberDMardell10:36 5 Sep '08  
GeneralVery nice PinmemberMR_SAM_PIPER14:14 7 May '08  
GeneralRe: Very nice PinmemberRalph Varjabedian21:19 7 May '08  
GeneralNot so nice though! PinmemberBalamurali Balaji22:51 2 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian2:33 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji4:24 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian6:59 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji8:11 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian11:12 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji18:00 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian1:03 4 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji3:41 4 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian5:05 4 May '08  
GeneralRe: Not so nice though! Pinmembernutwiss5:23 14 May '08  
GeneralRe: Not so nice though! Pinmemberrexahs6:38 7 Oct '08  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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