Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Dynamic Binding Of Hierarchy Data Structure To Treeview Control

, 29 Dec 2010
The article provides a code base for implementing dynamic hierarchical data structure with ASP.NET Treeview control
HierarchyTree.zip
HierarchyTree
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        PopulateTreeview();
      
     
    }
   

    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=xyz;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);
                }         
            }     
        }       
        foreach (HierarchyTrees.HTree hTree in hierarchyTrees)     
        {
            HierarchyTrees.HTree parentNode = hierarchyTrees.Find(delegate(HierarchyTrees.HTree emp) { return emp.NodeID == hTree.UnderParent; });
            if (parentNode != null)
            {         
                foreach (TreeNode tn in tvHierarchyView.Nodes)
                {
                    if (tn.Value == parentNode.NodeID.ToString())
                    {
                        tn.ChildNodes.Add(new TreeNode(hTree.NodeDescription.ToString(), hTree.NodeID.ToString()));
                    }
                    if (tn.ChildNodes.Count > 0)
                    {                    
                        foreach (TreeNode ctn in tn.ChildNodes)
                        {
                            RecursiveChild(ctn, parentNode.NodeID.ToString(), hTree);
                        }
                    }
                }                
            }
            else
            {               
                tvHierarchyView.Nodes.Add(new TreeNode(hTree.NodeDescription, hTree.NodeID.ToString()));
            } 
            }     
        tvHierarchyView.ExpandAll(); 
    }

    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);
            }
        }
    }
    

    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; }
            }
        } 
    }  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

santosh poojari
Technical Lead
India India
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 29 Dec 2010
Article Copyright 2010 by santosh poojari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid