Click here to Skip to main content
6,291,722 members and growing! (12,751 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

Binding ASP.NET TreeView to a DataSet or an ObjectDataSource

By Ralph Varjabedian

The TreeView can not bind to a DataSet or to an ObjectDataSource. With one line of code you can do that now.
C# (C# 1.0, C# 2.0, C# 3.0), Javascript, CSS, HTML, ASP, ASP.NET, Ajax, Dev
Posted:2 May 2008
Views:26,738
Bookmarked:24 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
9 votes for this article.
Popularity: 3.77 Rating: 3.95 out of 5
1 vote, 11.1%
1
1 vote, 11.1%
2

3
1 vote, 11.1%
4
6 votes, 66.7%
5

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.

Using the hood

The class HierarchicalDataSet presents data in a hierarchy, which means Father-Child relationship, 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 referencing 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


Member

Occupation: Chief Technology Officer
Company: Xplorium
Location: Lebanon Lebanon

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralUtterly awesome! Pinmemberdropit4:22 2 Oct '08  
GeneralRe: Utterly awesome! PinmemberRalph Varjabedian23:35 2 Oct '08  
GeneralSlight bug in this PinmemberDMardell3:35 2 Sep '08  
GeneralRe: Slight bug in this PinmemberRalph Varjabedian4:26 2 Sep '08  
GeneralRe: Slight bug in this PinmemberDMardell9:23 2 Sep '08  
GeneralRe: Slight bug in this PinmemberRalph Varjabedian23:37 2 Sep '08  
GeneralRe: Slight bug in this PinmemberDMardell11:36 5 Sep '08  
GeneralVery nice PinmemberMR_SAM_PIPER15:14 7 May '08  
GeneralRe: Very nice PinmemberRalph Varjabedian22:19 7 May '08  
GeneralNot so nice though! PinmemberBalamurali Balaji23:51 2 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian3:33 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji5:24 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian7:59 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji9:11 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian12:12 3 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji19:00 3 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian2:03 4 May '08  
GeneralRe: Not so nice though! PinmemberBalamurali Balaji4:41 4 May '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian6:05 4 May '08  
GeneralRe: Not so nice though! Pinmembernutwiss6:23 14 May '08  
GeneralRe: Not so nice though! Pinmemberrexahs7:38 7 Oct '08  
GeneralRe: Not so nice though! PinmemberRalph Varjabedian0:08 10 Oct '08  
GeneralWhy didn't Microsoft provide this? Can you make this work with custom collection? Pinmemberdefwebserver11:04 2 May '08  
GeneralRe: Why didn't Microsoft provide this? Can you make this work with custom collection? PinmemberRalph Varjabedian14:27 2 May '08  
GeneralRe: Why didn't Microsoft provide this? Can you make this work with custom collection? PinmemberRalph Varjabedian0:11 9 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2008
Editor:
Copyright 2008 by Ralph Varjabedian
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project