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

, 2 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
The TreeView can not bind to a DataSet or to an ObjectDataSource. With one line of code, you can do that now.


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:


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";

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

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

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

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

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

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.


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


About the Author

Ralph Varjabedian
Technical Lead
Lebanon Lebanon
No Biography provided

Comments and Discussions

Questiontam Pinmemberhienhoa4-Aug-13 18:41 
GeneralMy vote of 5 PinmemberD-Kishore4-Sep-12 19:44 
GeneralMy vote of 5 Pinmembershrikant.sherekar3-May-12 23:07 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Feb-12 1:40 
GeneralGreat Article, but what if I have checkboxes in my tree view by having this ShowCheckBoxes="All" Pinmemberrperetz12-Feb-10 10:19 
GeneralHi Ralph Varjabedian PinmemberMagesh Murugesan11-Jan-10 0:35 
GeneralRe: Hi Ralph Varjabedian PinmemberRalph Varjabedian11-Oct-13 7:44 
GeneralUtterly awesome! Pinmemberdropit2-Oct-08 4:22 
GeneralRe: Utterly awesome! PinmemberRalph Varjabedian2-Oct-08 23:35 
GeneralSlight bug in this PinmemberDMardell2-Sep-08 3:35 

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.

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