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:

Here is a quick example for some records to see how they will present the child parent relationship:

I have check boxes in my tree view, so I have one more field in my dataset for is_selected.
If the child or parent node is selected I want open the parent node.
Given that if the parent node is selected all the children are selected as well.

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.

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,

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

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!

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! 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.

Once data is made available in Xml form, whether it is in linear form or hierarchical or in a form that represents parent-child relationship, you may configure the treenode bindings property to reflect the same.

Pls. refer the msdn page that shows an example of using treeview and explains xmldatasource also.

I am looking at the page you sent me, it is a definition of the XmlDataSource class.
I still can not see how the data that you saved in XML which does not have child nodes (as xml), is going to be presented in the tree view based on two columns, (parent-child) relationship. Look at the data binding section that you sent me in the ASP.NET TreeView, how do you do that :confusd: there is only one way to bind to the XmlDataSource that it is simply: datamember and textfield. Have you done this before? have you seen the code somewhere, or are you just assuming that since a DataSet can be saved as an xml file and you can read it through XmlDataSource that it will automatically be done. I still can not see your solution. You are missing the main point that a DataSet when saved as XmlFile has a linear structure, it will not be saved as parent and child nodes!

Though this article does not shows how to use xml data, it shows how rows are loaded from the child dynamically, in the onexpanding event handler. Similarly, we could parse the xml-file created by the dataset, first with master table and then with child table on user-intervention which is more efficient. This is the logic I was thinking of when it comes to dataset-treeview binding. In your article, you have just shown how to use the series of IHierarchy interfaces to define a dataset suitable for tree-view binding.

And it shows a dataset which is static. Does it work for dynamic data retrieved from a database?

Right now, I dont have time work upon it and send you the copy of the results.

The method presented in my article is much easier, I would rather write one line of code than to do what the article you mentioned is doing handle events and for each TreeView control you add, there is just too much housekeeping compared with what I presented. As I said in the beginning there is no easy direct way to do this without doing extra coding. And even with your suggestion of XmlDataSource, you can not do that, putting the data in xml format is not enough to have the parent-child relationship. All you will have is linear data.

With my method, all you do is this:
TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");

Now unless there is something I am not seeing, I have introduced the easiet way to do this so far.

And I did not understand what you meant by static dataset You can bind my class with any dataset you want, dynamic or static, from database or from xml or from manually added rows or from loaded from file or deserialized! There is really no difference all you have to do it point my class to two column names.

When I have time next, I am going to do that with LINQ, so you will be able to bind the results of LINQ to a TreeView also with one line of code.

TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");
is quicker and easier.

But, remember where the dataset comes from. you need to build yourself a dataset implementing IHierarchy.. interfaces and populate the dataset in the code. This dataset I call it as static dataset( static data).

But in real-world applications, most of the times, dataset is created out of an existing datasource. Does your method works with dynamic data coming from a datasource? Do you have any ideas on combining such a dataset and IHierarchy?

Anyway, thank you very much for your co-operation in clarifying the article so far.

You're welcome for the clarification. My end target is for as much developers as possible to benefit from this.

I am not building a dataset myself that is implementing IHierarchy.... Please take a closed look at the code. The class that I am creating implements the Interface and keeps a reference to the passed in dataset. Anytime methods of the Interface are called, they are properly translated and the correct DataRowViews are returned that "describe" the parent-child data that should be returned. These decisions made by my class are based on the two column names that you pass in.

So to answer your question, yes my method works with any dynamic data coming from any data source. It does not matter as long as this data has two columns that create the circular reference that I talked about in my article that will help my class "see" the parent-child relationships.

I have explored your sample thoroughly and I could see only one table data in your dataset. If its a master-detail(parent-child) relationship stored in the dataset, it is supposed to have two tables in the dataset.

This means that you represent data in one table in hierarchical fashion. Within a table, you have assumed two columns as parent and child columns. But, not, displaying data from two tables related to each other by common column. If this is the case, where comes the parent-child relationship?

My understanding is that you are displaying data from parent and child table. I am still worried about this aspect.