5,699,997 members and growing! (23,218 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, C#), Javascript, CSS, HTML, Ajax, ASP, ASP.NET, Dev

Posted: 2 May 2008
Updated: 2 May 2008
Views: 14,304
Bookmarked: 19 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.17 Rating: 3.75 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
0 votes, 0.0%
3
1 vote, 14.3%
4
4 votes, 57.1%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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



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

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralUtterly awesome!memberdropit4:22 2 Oct '08  
GeneralRe: Utterly awesome!memberRalph Varjabedian23:35 2 Oct '08  
GeneralSlight bug in thismemberDMardell3:35 2 Sep '08  
GeneralRe: Slight bug in thismemberRalph Varjabedian4:26 2 Sep '08  
GeneralRe: Slight bug in thismemberDMardell9:23 2 Sep '08  
GeneralRe: Slight bug in thismemberRalph Varjabedian23:37 2 Sep '08  
GeneralRe: Slight bug in thismemberDMardell11:36 5 Sep '08  
GeneralVery nicememberMR_SAM_PIPER15:14 7 May '08  
GeneralRe: Very nicememberRalph Varjabedian22:19 7 May '08  
GeneralNot so nice though!memberBalamurali Balaji23:51 2 May '08  
GeneralRe: Not so nice though!memberRalph Varjabedian3:33 3 May '08  
GeneralRe: Not so nice though!memberBalamurali Balaji5:24 3 May '08  
GeneralRe: Not so nice though!memberRalph Varjabedian7:59 3 May '08  
GeneralRe: Not so nice though!memberBalamurali Balaji9:11 3 May '08  
GeneralRe: Not so nice though!memberRalph Varjabedian12:12 3 May '08  
GeneralRe: Not so nice though!memberBalamurali Balaji19:00 3 May '08  
GeneralRe: Not so nice though!memberRalph Varjabedian2:03 4 May '08  
GeneralRe: Not so nice though!memberBalamurali Balaji4:41 4 May '08  
GeneralRe: Not so nice though!memberRalph Varjabedian6:05 4 May '08  
GeneralRe: Not so nice though!membernutwiss6:23 14 May '08  
GeneralRe: Not so nice though!memberrexahs7:38 7 Oct '08  
GeneralRe: Not so nice though!memberRalph Varjabedian0:08 10 Oct '08  
GeneralWhy didn't Microsoft provide this? Can you make this work with custom collection?memberdefwebserver11:04 2 May '08  
GeneralRe: Why didn't Microsoft provide this? Can you make this work with custom collection?memberRalph Varjabedian14:27 2 May '08  
GeneralRe: Why didn't Microsoft provide this? Can you make this work with custom collection?memberRalph 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-2008
Web20 | Advertise on the Code Project