Table of Contents

ASP.NET Treeview control is the most used server control for representing tree view hierarchy data structure. Most of the time, we get requirements related to hierarchical tree structure of data representation; this becomes more complicated and complex if we've depth of leaf node to extreme extent. In such scenarios, we may have to look at generic solution. This article will help a developer to use the below logic or implementation to tackle similar situations.

Say for example, we've business problem statement of representing certain family or category in hierarchical fashion. In such case, iteration through XML tree or structured database becomes a challenge. Even parsing is one part of the problem and then representing it in graphical view of that hierarchy pattern.

Note: Here, the challenge is data structure are not hardcoded and any point of time new leaf node or parent node may be added at runtime. Hence having XML structure will be bit challenge as parsing, locating parent node and updating XML data will put additional overhead on programmer.

Create Stored procedure ssp_get_hierarchy: This will give Node_ID its child information and level or depth of leaf node or branch it belongs to.The below is the result set of the above stored procedure. The best use of Common table expression in SQL Server 2005 to refer recursive dataset using WITH Clause:

Create recursive function to traverse through hierarchical tree data structure. This functionality is developed using ASP.NET 2.0 and the recursive function can be avoided using CSharp .NET 3.5 using LINQ. Using anonymous delegate, we can compare the collection objects with some logical condition. Once the result set is filtered, it can be traversed and can be added as object item to tree view control.

Create recursive function to traverse through hierarchical tree data structure. This functionality is developed using ASP.NET 2.0 and the recursive function can be avoided using CSharp .NET 3.5 using LINQ. Using anonymous delegate, we can compare the collection objects with some logical condition. Once the result set is filtered, it can be traversed and can be added as object item to tree view control. Create HierarchyTrees generic collection which contains Htree objects.

The below reference is for Winform treeview control and I wrote the above article to make it available for ASP.NET treeview control as well. The important difference between winform and ASP.NET treeview control is FindNode.

In winform treeview control, we have tvHierarchyView.Nodes.Find where it finds a given node criteria and helps us add Htree object item at a given node value and hence one can use LINQ and can get rid of recursive function.

Unlike Winform, ASP.NET treeview control exposes tvHierarchyView.FindNode(XML Address path) which is more applicable for XML data type and hence not much of help.

I must confess that I didn't read complete article, but I see that you are not mentioning nowhere the IHierarchyData interface. I think that if you need to bind some data to a ASP.NET treeview your binding object must implement above mentioned interface. Here you can find some implementations examples http://www.codeproject.com/KB/custom-controls/IHierarchySupport.aspx.

The article you mentioned is excellent and addressed my problem.This what I m looking at. By using your reference I may avoid recursive function and my logic will resides in class itself without being bother to parse it in codebehid. All I need is to bind collection to tree view.
Thanks A Ton!. WISH U ALL A HAPPY NEW YEAR 2011

Glad I helped.
Note that IHierarchyData is part of System.Web.UI. So you will be able to bound only to classes that inherits from HierarchicalDataBoundControl. Note that no WinForm control inherits from that base class. There are many workarounds on web, make a research. Maybe a good suggestion for a next article?