Click here to Skip to main content
Licence 
First Posted 12 Aug 2002
Views 138,287
Downloads 2,278
Bookmarked 66 times

Populate a TreeView from a SELF-JOINED table

By Gevik Babakhani | 21 Nov 2002
This demo shows how to populate a treeview from a self-joined table
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
3.65/5 - 26 votes
μ 3.61, σa 2.68 [?]

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 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  
QuestionWhat is wrong with this piece of code ? PinmemberRamesh11046612:49 30 Sep '03  
GeneralNice but .... PinmemberEmad Rangchi1:27 14 Sep '03  
QuestionHow to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046620:20 13 Sep '03  
AnswerRe: 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    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
Web01 | 2.5.120210.1 | Last Updated 22 Nov 2002
Article Copyright 2002 by Gevik Babakhani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid