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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSlight bug in thismemberDMardell2 Sep '08 - 2:35 
Excellent code sample. Exactly what I was after!
 
However theres a small bug in the sample provided which throws with both the dataset you provide, and my own SQL datasource.
 
"public IEnumerator GetEnumerator()" throws StackOverflowException after getting stuck in an infinite loop as a result of "public bool HasChildren" always returning true.
 
Haven't quite got to the bottom of it yet but it seems to be stemming from the various cleardowns of the RowFilter you use for recursion. By Setting RowFilter="" its getting "stuck".
GeneralRe: Slight bug in this PinmemberRalph Varjabedian2 Sep '08 - 3:26 
I will check my dataset again because as I recall, my test case was working correctly...
As for your data, maybe the reason is that the parent IDs are not null but are set to zero to indicate (no parent). If you can please do pass me a sample of your data (if you want, just load them into a DataSet and call ExportXML) and send them to me and I will make sure I test your data and fix any possible bugs.
 
Thanks again.
 
Ralph Varjabedian
Chief Software specialist

My Blog
Bytesurge.com



GeneralRe: Slight bug in this PinmemberDMardell2 Sep '08 - 8:23 
My ParentID are also null. Was getting the same problem with the hardcoded DataSet in the example above as well.
GeneralRe: Slight bug in this PinmemberRalph Varjabedian2 Sep '08 - 22:37 
That is strange, I had someone report to me the same problem, however I do have it running normally on my machine. Since I can not reproduce the problem, I guess I can guide you to do some debugging. Put a breakpoint on lines that have the RowFilter. And try to see what is happening there.
Thanks,
 
Ralph Varjabedian
Chief Software specialist

My Blog
Bytesurge.com



GeneralRe: Slight bug in this PinmemberDMardell5 Sep '08 - 10:36 
Hi Ralph. I did spend quite a bit of time debugging this before I posted - figued I could fix it on my own but I was wrong Smile | :)
 
Found that by eliminating the 'RowFilter=""' statements that I escaped the infinite loop - which was nice!
 
However this threw up more porblems (expectedly) with errors of "No row at position 0" when reaching the last child in the datasource (be it my database or your hard coded sample)
 
I suspect it may be something with my set up. If I download the code sample provided and try and open the project file VS returns an error "not supported by this instalation". I had to create a new project and start adding the files in manually to get it going - which might be why it works for you and not me!
 
As it stands I gave up and found an alternative solution. I now load the dataset from the DB and using a recursive call to a method that uses the same principle as your "public IEnumerator GetEnumerator()" I generate a string of "Hierachical" XML
 
From there its just a case of setting an "XMLDataSource.Data = xmlString" and then binding the treeView to that XMLDataSource.
 
Not nesisarily a BETTER soloution - but I got it going. And I would never have had the inspiration if not for this article. So for that - I thank you. Saved me a big headache!

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

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