Dynamic Binding Of Hierarchy Data structure To Treeview Control
Table of Contents
- Introduction
- Business Requirement
- Solution
- Database Implementation
- Recursive Function
- Conclusion
- Reference
Introduction
Asp.net Treeview control is most used server control for representing tree view hierarchy data structure. Most of the time we get requirement 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 developer to use the below logic or implementation to tackle similar situation.
Business Requirement
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 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.
Solution
Approach to be followed:
Important contributing factors are here to create well data table structure and binding it to tree view control. Asp.net TreeView Control.
Technology
ASP.net
SQL Server 2005
Database Implementation
Create table tbl_Tree_Hierarchy

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 above stored procedure. The best use of Common table expression in SQL Server 2005 to refer recursive dataset using WITH Clause

Recursive Function
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. public class HierarchyTrees : List <HierarchyTrees.HTree>
{
public class HTree
{
private string m_NodeDescription;
private int m_UnderParent;
private int m_LevelDepth;
private int m_NodeID;
public int NodeID
{ get {return m_NodeID;}
set { m_NodeID=value; }
}
public string NodeDescription
{
get { return m_NodeDescription; }
set { m_NodeDescription = value; }
}
public int UnderParent
{
get { return m_UnderParent; }
set { m_UnderParent = value; }
}
public int LevelDepth
{
get { return m_LevelDepth; }
set { m_LevelDepth = value; }
}
}
}
PopulateTreeview() function will fetch recordset from database and will populate the generic collection tree.
private void PopulateTreeview() {
this.tvHierarchyView.Nodes.Clear();
HierarchyTrees hierarchyTrees = new HierarchyTrees();
HierarchyTrees.HTree objHTree=null;
using (SqlConnection connection = new SqlConnection(@"Persist Security Info=False;Integrated Security=SSPI;database=FamilyTree;server=[Local]"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SSP_GET_HIERARCHY", connection))
{ command.CommandType = System.Data.CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (reader.Read())
{
objHTree=new HierarchyTrees.HTree();
objHTree.LevelDepth = int.Parse(reader["LEVEL_DEPTH"].ToString());
objHTree.NodeID = int.Parse(reader["NODE_ID"].ToString());
objHTree.UnderParent = int.Parse(reader["UNDER_PARENT"].ToString());
objHTree.NodeDescription = reader["NODE_DESCRIPTION"].ToString();
hierarchyTrees.Add(objHTree);
}
}
}
//Iterate through Collections.
foreach (HierarchyTrees.HTree hTree in hierarchyTrees)
{
//Filter the collection HierarchyTrees based on Iteration as per object Htree Parent ID
HierarchyTrees.HTree parentNode = hierarchyTrees.Find(delegate(HierarchyTrees.HTree emp) { return emp.NodeID == hTree.UnderParent; });
//If parent node has child then populate the leaf node.
if (parentNode != null)
{
foreach (TreeNode tn in tvHierarchyView.Nodes)
{
//If single child then match Node ID with Parent ID
if (tn.Value == parentNode.NodeID.ToString())
{
tn.ChildNodes.Add(new TreeNode(hTree.NodeDescription.ToString(), hTree.NodeID.ToString()));
}
//If Node has multiple child ,recursively traverse through end child or leaf node.
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode ctn in tn.ChildNodes)
{
RecursiveChild(ctn, parentNode.NodeID.ToString(), hTree);
}
}
}
}
//Else add all Node at first level
else
{
tvHierarchyView.Nodes.Add(new TreeNode(hTree.NodeDescription, hTree.NodeID.ToString()));
}
}
tvHierarchyView.ExpandAll();
}
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.
public void RecursiveChild(TreeNode tn, string searchValue, HierarchyTrees.HTree hTree)
{
if (tn.Value == searchValue)
{
tn.ChildNodes.Add(new TreeNode(hTree.NodeDescription.ToString(), hTree.NodeID.ToString()));
}
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode ctn in tn.ChildNodes)
{
RecursiveChild(ctn, searchValue,hTree);
}
}
}
References
The below reference is for win form treeview control and I wrote above article to make it available for asp.net tree view 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 help us add Htree object item at 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.
Conclusion
Any ideas, corrections, and suggestions are most welcome.
发表评论
fjqE97 It's really a cool and useful piece of info. I am satisfied that you just shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.
h0vqoN wow, awesome blog post.Much thanks again. Really Cool.
mMtEZP Really appreciate you sharing this article.Really thank you!