Click here to Skip to main content
12,507,845 members (50,072 online)
Click here to Skip to main content
Add your own
alternative version

Stats

155.4K views
2.7K downloads
66 bookmarked
Posted

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)



You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionVB Code Pin
r. salehi23-Aug-12 1:05
memberr. salehi23-Aug-12 1:05 
AnswerRe: VB Code Pin
Gevik Babakhani23-Aug-12 1:35
groupGevik Babakhani23-Aug-12 1:35 
GeneralRe: VB Code Pin
r. salehi24-Aug-12 1:41
memberr. salehi24-Aug-12 1:41 
GeneralRe: VB Code Pin
r. salehi24-Aug-12 19:12
memberr. salehi24-Aug-12 19:12 
GeneralMy vote of 4 Pin
Member 641742225-Jul-12 5:32
memberMember 641742225-Jul-12 5:32 
GeneralHi Pin
Bino B4-Dec-07 19:29
memberBino B4-Dec-07 19:29 
Generalgrr Pin
dscanlon11-May-07 3:16
memberdscanlon11-May-07 3:16 
GeneralThanks Pin
Neeraj Arora21-Feb-07 0:51
memberNeeraj Arora21-Feb-07 0:51 
GeneralNice Very Nice Pin
nemisar23-Jun-06 1:17
membernemisar23-Jun-06 1:17 
GeneralDrag & Drop Pin
TLWallace5-Jan-04 9:00
memberTLWallace5-Jan-04 9:00 
GeneralThanks! Pin
demerson22-Dec-03 18:22
memberdemerson22-Dec-03 18:22 
QuestionWhat is wrong with this piece of code ? Pin
Ramesh11046630-Sep-03 11:49
memberRamesh11046630-Sep-03 11:49 
GeneralNice but .... Pin
Emad Rangchi14-Sep-03 0:27
memberEmad Rangchi14-Sep-03 0:27 
QuestionHow to populate a windows treeview control using c# from a relational table without using xml Pin
Ramesh11046613-Sep-03 19:20
memberRamesh11046613-Sep-03 19:20 
AnswerRe: How to populate a windows treeview control using c# from a relational table without using xml Pin
Gevik Babakhani13-Sep-03 22:59
memberGevik Babakhani13-Sep-03 22:59 
GeneralRe: How to populate a windows treeview control using c# from a relational table without using xml Pin
Ramesh11046614-Sep-03 14:14
memberRamesh11046614-Sep-03 14:14 
GeneralAdding data Pin
Donald Blachly11-Sep-02 19:20
memberDonald Blachly11-Sep-02 19:20 
GeneralRe: Adding data Pin
d_vaibhav20-Aug-04 20:56
memberd_vaibhav20-Aug-04 20:56 
GeneralGood by may be some troubles... Pin
Alex Kucherenko19-Aug-02 21:46
memberAlex Kucherenko19-Aug-02 21:46 
GeneralRe: Good by may be some troubles... Pin
Gevik Babakhani19-Aug-02 21:49
memberGevik Babakhani19-Aug-02 21:49 
GeneralRe: Good by may be some troubles... Pin
Alex Kucherenko19-Aug-02 22:05
memberAlex Kucherenko19-Aug-02 22:05 
GeneralRe: Good by may be some troubles... Pin
BiGbrother13sgg7-Oct-05 0:08
sussBiGbrother13sgg7-Oct-05 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 22 Nov 2002
Article Copyright 2002 by Gevik Babakhani
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid