Click here to Skip to main content
Click here to Skip to main content
Go to top

Populate a TreeView from a SELF-JOINED table

, 21 Nov 2002
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Gevik Babakhani
Web Developer
Netherlands Netherlands
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.
Group type: Organisation (No members)



Comments and Discussions

 
QuestionVB Code Pinmemberr. salehi23-Aug-12 1:05 
AnswerRe: VB Code PingroupGevik Babakhani23-Aug-12 1:35 
GeneralRe: VB Code Pinmemberr. salehi24-Aug-12 1:41 
GeneralRe: VB Code Pinmemberr. salehi24-Aug-12 19:12 
GeneralMy vote of 4 PinmemberMember 641742225-Jul-12 5:32 
GeneralHi PinmemberBino B4-Dec-07 19:29 
Generalgrr Pinmemberdscanlon11-May-07 3:16 
GeneralThanks PinmemberNeeraj Arora21-Feb-07 0:51 
GeneralNice Very Nice Pinmembernemisar23-Jun-06 1:17 
GeneralDrag & Drop PinmemberTLWallace5-Jan-04 9:00 
GeneralThanks! Pinmemberdemerson22-Dec-03 18:22 
QuestionWhat is wrong with this piece of code ? PinmemberRamesh11046630-Sep-03 11:49 
GeneralNice but .... PinmemberEmad Rangchi14-Sep-03 0:27 
QuestionHow to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046613-Sep-03 19:20 
AnswerRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberGevik Babakhani13-Sep-03 22:59 
GeneralRe: How to populate a windows treeview control using c# from a relational table without using xml PinmemberRamesh11046614-Sep-03 14:14 
GeneralAdding data PinmemberDonald Blachly11-Sep-02 19:20 
GeneralRe: Adding data Pinmemberd_vaibhav20-Aug-04 20:56 
GeneralGood by may be some troubles... PinmemberAlex Kucherenko19-Aug-02 21:46 
GeneralRe: Good by may be some troubles... PinmemberGevik Babakhani19-Aug-02 21:49 
GeneralRe: Good by may be some troubles... PinmemberAlex Kucherenko19-Aug-02 22:05 
GeneralRe: Good by may be some troubles... PinsussBiGbrother13sgg7-Oct-05 0:08 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Nov 2002
Article Copyright 2002 by Gevik Babakhani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid