Click here to Skip to main content
15,867,330 members
Articles / Web Development / ASP.NET
Article

Populate a TreeView from a SELF-JOINED table

Rate me:
Please Sign up or sign in to vote.
4.20/5 (23 votes)
21 Nov 2002 173.7K   2.9K   68   22
This demo shows how to populate a treeview from a self-joined table

Image 1

Image 2

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

C#
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

C#
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


Written By
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.
This is a Organisation (No members)


Comments and Discussions

 
QuestionVB Code Pin
r. salehi23-Aug-12 1:05
r. salehi23-Aug-12 1:05 
AnswerRe: VB Code Pin
Gevik Babakhani23-Aug-12 1:35
Gevik Babakhani23-Aug-12 1:35 
GeneralRe: VB Code Pin
r. salehi24-Aug-12 1:41
r. salehi24-Aug-12 1:41 
GeneralRe: VB Code Pin
r. salehi24-Aug-12 19:12
r. salehi24-Aug-12 19:12 
GeneralMy vote of 4 Pin
Member 641742225-Jul-12 5:32
Member 641742225-Jul-12 5:32 
GeneralHi Pin
Bino B4-Dec-07 19:29
Bino B4-Dec-07 19:29 
Generalgrr Pin
dscanlon11-May-07 3:16
dscanlon11-May-07 3:16 
GeneralThanks Pin
Neeraj Arora21-Feb-07 0:51
Neeraj Arora21-Feb-07 0:51 
GeneralNice Very Nice Pin
nemisar23-Jun-06 1:17
nemisar23-Jun-06 1:17 
GeneralDrag & Drop Pin
TLWallace5-Jan-04 9:00
TLWallace5-Jan-04 9:00 
GeneralThanks! Pin
demerson22-Dec-03 18:22
demerson22-Dec-03 18:22 
QuestionWhat is wrong with this piece of code ? Pin
Ramesh11046630-Sep-03 11:49
Ramesh11046630-Sep-03 11:49 
GeneralNice but .... Pin
Emad Rangchi14-Sep-03 0:27
Emad 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
Ramesh11046613-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
Gevik 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
Ramesh11046614-Sep-03 14:14 
GeneralAdding data Pin
Donald Blachly11-Sep-02 19:20
Donald Blachly11-Sep-02 19:20 
GeneralRe: Adding data Pin
d_vaibhav20-Aug-04 20:56
d_vaibhav20-Aug-04 20:56 
GeneralGood by may be some troubles... Pin
Oleksandr Kucherenko19-Aug-02 21:46
Oleksandr Kucherenko19-Aug-02 21:46 
GeneralRe: Good by may be some troubles... Pin
Gevik Babakhani19-Aug-02 21:49
Gevik Babakhani19-Aug-02 21:49 
GeneralRe: Good by may be some troubles... Pin
Oleksandr Kucherenko19-Aug-02 22:05
Oleksandr 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.