Database
  Home arrow Database arrow Page 4 - TreeView Part 1 - Populating the New TreeV...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2005-06-08

    Table of Contents:
  • TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table
  • Step 1 – Drag ‘n Drop
  • Step 2 - Populating the Data
  • Step 3 - Building the Tree

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    TreeView Part 1 - Populating the New TreeView Control from a Hierarchical Database Table - Step 3 - Building the Tree


    (Page 4 of 4 )

    To explain first of all, we need to build the tree node by node. We specify to start with the Tree’s root node set, and start with a parent id of ‘0’. This would be the president of the company, the home directory of a site, the root of a product list, etc. Then the DataSet is searched for any items with that parent id (in this case, the ‘mgr_id’). Any found are placed into the tree as a node. Then the function calls itself with that employee’s id as the parent id. Any children (reporting employees, sub-directories) are populated, and the recursion takes place for each. Each time we get to a node with no children (a leaf node) the function exits, and continues populating children nodes for the previous parent, all the way back to the root. Now here’s all that in code:

    private void BuildEmployeeList(TreeNodeCollection nodes,
    Int32 IntParent)
    {

        Int32 ThisID;

        String ThisName;

        DataRow[] children =
            EmployeeDS.Tables["employees"].Select
            ("mgr_id='"+IntParent+"'");

        //no child nodes, exit function
       
    if (children.Length == 0) return;

        foreach (DataRow child in children)
        {
            // step 1
            ThisID = Convert.ToInt32(child.ItemArray[0]);

            // step 2
            ThisName = Convert.ToString(child.ItemArray[1]);

            // step 3
            TreeNode NewNode =
               
    new TreeNode(ThisName, ThisID.ToString());

            // step 4
            nodes.Add(NewNode);
            NewNode.ToggleExpandState();

            // step 5
            BuildEmployeeList(NewNode.ChildNodes, ThisID);
        }
    }

    Rather than break up the code with explanation, here’s each step explained so that you can understand what’s happening where, in case you need to modify or debug the code for you own purposes.

    Step #

    1. Here we retrieve the id of the current employee for use in creating the node and feeding back in the recursive call to find any subordinates.
    2. The name is retrieved, more for display purposes than anything functional.
    3. Here a new TreeNode is created on the fly, with the name as the display text, and the id as the string value. This is necessary for the SelectEmployee method we created earlier. Within that method, you would parse that unique id value, and use it to retrieve the information on that specific employee from the database.
    4. The node is added to the Tree. Then we toggle the expanded state, which both sets it to collapsed, and actually gives it an expanded property value. This will save you many headaches later.
    5. Now we recursively call the same method, now using the current employee’s id as the parent id, and the current node as the parent node. If no children are found, the method will simply move on to the next ‘sibling’ node.

    Conclusion

    There you have it, a fully functioning TreeView. Well, functioning in that it will actually display the employee data in a hierarchical manner when you load the page.

    There is still much functionality we could add. In my next article, I’ll explain how to select a specific employee from the list, perhaps by a variable passed in the query string, and retrieve data for that employee. Until then, have fun!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · It says in the article "you will have no trouble converting it into VB".Wrong. ...
       · A waste of time??The aritcle describes the process and principles behind the...
       · very good article, thanks very much :)
       · It is a good article but very high level cause was hoping to see more complex...
       · what does the mgr_id mean? please help
       · Was a wonderful article and it really helped me.Thank you. Was wondering if we...
       · 1: I never knew C-sharp had a project much more complex than this and converted it...
       · I get an error that specifies that it can't find EmployeeListin the Page_Load...
     

    DATABASE ARTICLES

    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT