Click here to Skip to main content
6,929,467 members and growing! (15,452 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Populate a TreeView from a SELF-JOINED table

By Gevik Babakhani

This demo shows how to populate a treeview from a self-joined table
C#, Windows, .NET1.0, ASP.NET, Visual-Studio, DBA, Dev
Posted:12 Aug 2002
Updated:21 Nov 2002
Views:126,566
Bookmarked:63 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 5.10 Rating: 3.61 out of 5
4 votes, 18.2%
1
1 vote, 4.5%
2
4 votes, 18.2%
3
4 votes, 18.2%
4
9 votes, 40.9%
5

Introduction

This very simple example provides a demonstration of how one can populate a TreeView from a self-joined database table. In this example I have used a DataSet containing one DataTable that is populated with virtual XML data.

Core

The SimpleDataSet class provides a DataTable populated with self-joined data. The SimpleTreeView the actual data consumer object.

Sample of how the TreeView is populated

public void CreateNodesOfParent(int iParent,TreeNode pNode) {
            
    DataView dvwData = new DataView(sdsData.Tables[0]);

    dvwData.RowFilter =  "[PARENT] = " + iParent;
    
    foreach(DataRowView Row in dvwData) {
                

        // Call Stack !

        if(pNode == null) {
            TreeNode zNode = this.Nodes.Add(Row["Caption"].ToString());
            CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);
        } else {
            TreeNode zNode =  pNode.Nodes.Add(Row["Caption"].ToString());
            CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);    
        }
                        
    }            
}        

Sample of how the DataTable is populated

private void CreateDummyData() {
            
    //DataRow objRow;

    // Creating columns

    prvTable = new DataTable("tblTreeView");
    prvTable.Columns.Add("ID",typeof(Int32));
    prvTable.Columns.Add("PARENT",typeof(Int32));
    prvTable.Columns.Add("CAPTION",typeof(String));
            
    // Adding Table to DataSet

    this.Tables.Add(prvTable);
            
                
    prvTable.Rows.Add(new Object[] {1,0,"Countries"});
        prvTable.Rows.Add(new Object[] {2,1,"Netherlands"});
            prvTable.Rows.Add(new Object[] {3,2,"Cities"});            
            prvTable.Rows.Add(new Object[] {4,3,"The Hague"});
            prvTable.Rows.Add(new Object[] {5,3,"Amsterdam"});
            prvTable.Rows.Add(new Object[] {6,3,"Rotterdam"});
    ......
    ......            
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gevik Babakhani


Member
Gevik Babakhani is a software architect currently residing in The Netherlands. He started programming as hobby from the age of 12. His professional career began in 1997. Since then he works fulltime as a software architect. Currently he dedicates time and effort in Microsoft.NET.
Occupation: Web Developer
Location: Netherlands Netherlands

Other popular Database articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralHi PinmemberBino B20:29 4 Dec '07  
Generalgrr Pinmemberdscanlon4:16 11 May '07  
GeneralThanks PinmemberNeeraj Arora1:51 21 Feb '07  
GeneralNice Very Nice Pinmembernemisar2:17 23 Jun '06  
GeneralDrag & Drop PinmemberTLWallace10:00 5 Jan '04  
GeneralThanks! Pinmemberdemerson19:22 22 Dec '03  
GeneralWhat is wrong with this piece of code ? PinmemberRamesh11046612:49 30 Sep '03  
GeneralNice but .... PinmemberEmad Rangchi1:27 14 Sep '03  
GeneralHow to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046620:20 13 Sep '03  
GeneralRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberGevik Babakhani23:59 13 Sep '03  
GeneralRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046615:14 14 Sep '03  
GeneralAdding data PinmemberDonald Blachly20:20 11 Sep '02  
GeneralRe: Adding data Pinmemberd_vaibhav21:56 20 Aug '04  
GeneralGood by may be some troubles... PinmemberAlex Kucherenko22:46 19 Aug '02  
GeneralRe: Good by may be some troubles... PinmemberGevik Babakhani22:49 19 Aug '02  
GeneralRe: Good by may be some troubles... PinmemberAlex Kucherenko23:05 19 Aug '02  
GeneralRe: Good by may be some troubles... PinsussBiGbrother13sgg1:08 7 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 21 Nov 2002
Editor: Nishant Sivakumar
Copyright 2002 by Gevik Babakhani
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project