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
Member
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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberD-Kishore4 Sep '12 - 18:44 
GeneralMy vote of 5membershrikant.sherekar3 May '12 - 22:07 
GeneralMy vote of 5membermanoj kumar choubey16 Feb '12 - 0:40 
GeneralGreat Article, but what if I have checkboxes in my tree view by having this ShowCheckBoxes="All"memberrperetz12 Feb '10 - 9:19 
GeneralHi Ralph VarjabedianmemberMagesh Murugesan10 Jan '10 - 23:35 
GeneralUtterly awesome!memberdropit2 Oct '08 - 3:22 
Thanks, this saved my day!!! Cool | :cool:
GeneralRe: Utterly awesome!memberRalph Varjabedian2 Oct '08 - 22:35 
GeneralSlight bug in thismemberDMardell2 Sep '08 - 2:35 
GeneralRe: Slight bug in thismemberRalph Varjabedian2 Sep '08 - 3:26 
GeneralRe: Slight bug in thismemberDMardell2 Sep '08 - 8:23 
GeneralRe: Slight bug in thismemberRalph Varjabedian2 Sep '08 - 22:37 
GeneralRe: Slight bug in thismemberDMardell5 Sep '08 - 10:36 
GeneralVery nicememberMR_SAM_PIPER7 May '08 - 14:14 
GeneralRe: Very nicememberRalph Varjabedian7 May '08 - 21:19 
GeneralNot so nice though!memberBalamurali Balaji2 May '08 - 22:51 
GeneralRe: Not so nice though!memberRalph Varjabedian3 May '08 - 2:33 
GeneralRe: Not so nice though!memberBalamurali Balaji3 May '08 - 4:24 
GeneralRe: Not so nice though!memberRalph Varjabedian3 May '08 - 6:59 
GeneralRe: Not so nice though!memberBalamurali Balaji3 May '08 - 8:11 
GeneralRe: Not so nice though!memberRalph Varjabedian3 May '08 - 11:12 
GeneralRe: Not so nice though!memberBalamurali Balaji3 May '08 - 18:00 
GeneralRe: Not so nice though!memberRalph Varjabedian4 May '08 - 1:03 
GeneralRe: Not so nice though!memberBalamurali Balaji4 May '08 - 3:41 
GeneralRe: Not so nice though!memberRalph Varjabedian4 May '08 - 5:05 
GeneralRe: Not so nice though!membernutwiss14 May '08 - 5:23 

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

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