Click here to Skip to main content
15,867,704 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.70/5 (17 votes)
1 May 2008CPOL1 min read 148.2K   4.1K   32   32
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:

C#
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.

C#
TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");

Hope this helps.

History

  • 2nd May, 2008: Initial version

License

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


Written By
Technical Lead
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontam Pin
hienhoa4-Aug-13 17:41
hienhoa4-Aug-13 17:41 
GeneralMy vote of 5 Pin
D-Kishore4-Sep-12 18:44
D-Kishore4-Sep-12 18:44 
GeneralMy vote of 5 Pin
shrikant.sherekar3-May-12 22:07
shrikant.sherekar3-May-12 22:07 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 0:40
professionalManoj Kumar Choubey16-Feb-12 0:40 
GeneralGreat Article, but what if I have checkboxes in my tree view by having this ShowCheckBoxes="All" Pin
rperetz12-Feb-10 9:19
rperetz12-Feb-10 9:19 
GeneralHi Ralph Varjabedian Pin
Magesh Murugesan10-Jan-10 23:35
Magesh Murugesan10-Jan-10 23:35 
GeneralRe: Hi Ralph Varjabedian Pin
Ralph Varjabedian11-Oct-13 6:44
Ralph Varjabedian11-Oct-13 6:44 
GeneralUtterly awesome! Pin
dropit2-Oct-08 3:22
dropit2-Oct-08 3:22 
GeneralRe: Utterly awesome! Pin
Ralph Varjabedian2-Oct-08 22:35
Ralph Varjabedian2-Oct-08 22:35 
GeneralSlight bug in this Pin
DMardell2-Sep-08 2:35
DMardell2-Sep-08 2:35 
GeneralRe: Slight bug in this Pin
Ralph Varjabedian2-Sep-08 3:26
Ralph Varjabedian2-Sep-08 3:26 
GeneralRe: Slight bug in this Pin
DMardell2-Sep-08 8:23
DMardell2-Sep-08 8:23 
GeneralRe: Slight bug in this Pin
Ralph Varjabedian2-Sep-08 22:37
Ralph Varjabedian2-Sep-08 22:37 
GeneralRe: Slight bug in this Pin
DMardell5-Sep-08 10:36
DMardell5-Sep-08 10:36 
GeneralVery nice Pin
MR_SAM_PIPER7-May-08 14:14
MR_SAM_PIPER7-May-08 14:14 
GeneralRe: Very nice Pin
Ralph Varjabedian7-May-08 21:19
Ralph Varjabedian7-May-08 21:19 
GeneralNot so nice though! Pin
Balamurali Balaji2-May-08 22:51
Balamurali Balaji2-May-08 22:51 
GeneralRe: Not so nice though! Pin
Ralph Varjabedian3-May-08 2:33
Ralph Varjabedian3-May-08 2:33 
Thank you for sharing,

Even though the TreeView control supports XmlDataSource and even if you save the file as xml and read it into an XmlDataSource, how are you going to bind the linear structure to a parent-child relationship! Confused | :confused: Please if you have some code or a url that shows this in action, let us know. Thank you. The binding problem is not just type based (can not bind to a DataSet) it is also logical based (can not bind to a linear structure). My class takes two column names and generates this parent-child structure for you.

Ralph Varjabedian
Chief Software specialist

My Blog
Bytesurge.com



GeneralRe: Not so nice though! Pin
Balamurali Balaji3-May-08 4:24
Balamurali Balaji3-May-08 4:24 
GeneralRe: Not so nice though! Pin
Ralph Varjabedian3-May-08 6:59
Ralph Varjabedian3-May-08 6:59 
GeneralRe: Not so nice though! Pin
Balamurali Balaji3-May-08 8:11
Balamurali Balaji3-May-08 8:11 
GeneralRe: Not so nice though! Pin
Ralph Varjabedian3-May-08 11:12
Ralph Varjabedian3-May-08 11:12 
GeneralRe: Not so nice though! Pin
Balamurali Balaji3-May-08 18:00
Balamurali Balaji3-May-08 18:00 
GeneralRe: Not so nice though! Pin
Ralph Varjabedian4-May-08 1:03
Ralph Varjabedian4-May-08 1:03 
GeneralRe: Not so nice though! Pin
Balamurali Balaji4-May-08 3:41
Balamurali Balaji4-May-08 3:41 

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

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