Click here to Skip to main content
Licence 
First Posted 12 Aug 2002
Views 139,869
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  
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  
Try to modify code in such case:
- do not call function in recursion
- use Sustem.Collections.Stack class
- Stack methods Push and Pop can be used as a analog of recursion
 
When recursion end condition is true then Stack.Pop(...) methos must be called, otherwise call Stack.Push(...) method. By such method calls you can emulate function recursion...
 
Sorry but i have not too much time to show this in example...
But I have example which use STL Stack() class, maybe this can make my suggestions more effective and understandable:

//////////////////////////////////////////////////////////////////////////
// Function creater threaad and start find process ...
 
HANDLE CFindFile::FindFiles( void )
{
DWORD tid = 0;
m_dwError = 0;
 
m_stackPath.push( new CDirectoryStore( m_strStartPoint ) );
 
m_hThread = ::CreateThread( NULL, 0,
( LPTHREAD_START_ROUTINE )FindDirFileThread, ( LPVOID )this,
CREATE_SUSPENDED, &tid );
 

if( m_hThread == NULL )
m_dwError = GetLastError();
else
{
if( ::SetThreadPriority( m_hThread, THREAD_PRIORITY_HIGHEST ) == 0 )
m_dwError = GetLastError();
 
if( ::ResumeThread( m_hThread ) < 0 )
m_dwError = GetLastError();
}

return m_hThread;
};
 
//////////////////////////////////////////////////////////////////////////
//
 
DWORD CFindFile::FindDirFileThread( CFindFile *pThis )
{
if( pThis->m_stackPath.size() == 0 )
ExitThread( 0 );

CFuncLog log( pThis->m_pLog, "CFindFile::FindDirFileThread" );

WIN32_FIND_DATA tFind;
string tmpPath;
HANDLE hFind = NULL;
char ext[_MAX_EXT];
string tmpExt = "." + pThis->m_strExt;

while( pThis->m_stackPath.size() > 0 )
{
std::auto_ptr< CDirectoryStore > pDir( pThis->m_stackPath.front() );

tmpPath = pDir->GetRoot() + "\\*";

log.LogString( LOG_INFO, "Current Root: " + pDir->GetRoot() + "\n" );

// NOTE: -- here we skip first directory - because it's always must
// be equel to '.' directory --
hFind = ::FindFirstFile( tmpPath.c_str(), &tFind );
if( hFind == INVALID_HANDLE_VALUE )
{
PSHARE_INFO_502 BufPtr = NULL, p;
NET_API_STATUS res;
LPTSTR lpszServer = NULL;
DWORD er=0, tr=0, resume=0;
_bstr_t path( pDir->GetRoot().c_str() );

res = ::NetShareEnum( path, 502, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
 
if( res == ERROR_SUCCESS || res == ERROR_MORE_DATA )
{
p = BufPtr;
 
for( int j=0; j < er; j++ )
{
//if( ::IsValidSecurityDescriptor( p->shi502_security_descriptor ) != 0 )
//{
_bstr_t netname( p->shi502_netname );
UINT lLen = netname.length();
 
// Skip hidden shares
if( ( ( char * )netname )[ lLen-1 ] != '$' )
{
pThis->m_stackPath.push(
new CDirectoryStore( pDir->GetRoot() + "\\" + string( netname ) ) );
 
log << "Network Share Folder:" << _bstr_t( p->shi502_netname );
}
//}
 
p++;
}

::NetApiBufferFree( BufPtr );
}
else
{
pThis->m_stackPath.pop();
continue;
}
}

while( 1 )
{
if( ( tFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) > 0 )
{
if( strcmp( tFind.cFileName, "." ) != 0 && strcmp( tFind.cFileName, ".." ) != 0 )
{
pThis->m_stackPath.push(
new CDirectoryStore( pDir->GetRoot() + "\\" + string( tFind.cFileName ) ) );
}
}
else
{
_splitpath( tFind.cFileName, NULL, NULL, NULL, ext );

if( stricmp( ext, tmpExt.c_str() ) == 0 )
pDir->AddFile( tFind.cFileName );
}

if( FindNextFile( hFind, &tFind ) == FALSE ) break;
}
 
if( pDir->size() > 0 )
pThis->m_listFiles.push_back( pDir.release() );
 
pThis->m_stackPath.pop();
 
FindClose( hFind );
hFind = NULL;
}

return 0;
};

 
Sorry for not very clear example, it just a code which I use in Fast File Find algorithm

 
Alex Kucherenko
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
Web03 | 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