Click here to Skip to main content
Licence 
First Posted 12 Aug 2002
Views 139,842
Bookmarked 66 times

Populate a TreeView from a SELF-JOINED table

By | 21 Nov 2002 | Article
This demo shows how to populate a treeview from a self-joined table

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

Web Developer

Netherlands Netherlands

Member



Organisation (No members)

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralHi PinmemberBino B19:29 4 Dec '07  
Generalgrr Pinmemberdscanlon3:16 11 May '07  
GeneralThanks PinmemberNeeraj Arora0:51 21 Feb '07  
Thanks buddy,
It has saved my a lot of time.
GeneralNice Very Nice Pinmembernemisar1:17 23 Jun '06  
GeneralDrag & Drop PinmemberTLWallace9:00 5 Jan '04  
GeneralThanks! Pinmemberdemerson18:22 22 Dec '03  
QuestionWhat is wrong with this piece of code ? PinmemberRamesh11046611:49 30 Sep '03  
GeneralNice but .... PinmemberEmad Rangchi0:27 14 Sep '03  
QuestionHow to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046619:20 13 Sep '03  
AnswerRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberGevik Babakhani22:59 13 Sep '03  
GeneralRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046614:14 14 Sep '03  
GeneralAdding data PinmemberDonald Blachly19:20 11 Sep '02  
GeneralRe: Adding data Pinmemberd_vaibhav20:56 20 Aug '04  
GeneralGood by may be some troubles... PinmemberAlex Kucherenko21:46 19 Aug '02  
GeneralRe: Good by may be some troubles... PinmemberGevik Babakhani21:49 19 Aug '02  
GeneralRe: Good by may be some troubles... PinmemberAlex Kucherenko22:05 19 Aug '02  
GeneralRe: Good by may be some troubles... PinsussBiGbrother13sgg0:08 7 Oct '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 22 Nov 2002
Article Copyright 2002 by Gevik Babakhani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid