Click here to Skip to main content
13,151,815 members (61,137 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 20 Mar 2006

C# TreeView Traversing

, 21 Aug 2006
Rate this:
Please Sign up or sign in to vote.
This article presents a simple c# TreeView traversal mechanism.


TreeView in C# is a great control. TreeView can be used to build Hierarchical Menus. I was involved in a project where i had to make use of TreeView quite heavily and as a result i wrote some code myself to Build the TreeView dynamically (database driven). Here i present the TreeView traversal operation.

Here's a snapshot of the Populated TreeView.

Download sourcecode

Sample screenshot

















Here is the code:

class</FONT> TVIEW


<FONT color=#008000 size=1></P><P>//1- Create a TreeView</P></FONT><FONT size=1><P>TreeView treeview = </FONT><FONT color=#0000ff size=1>new</FONT><FONT size=1> TreeView();
</FONT><FONT size=1></P><P></FONT><FONT color=#008000 size=1></FONT> </P><P><FONT color=#008000 size=1>//2- Populate the TreeView with Data</P></FONT><FONT size=1><P></FONT><FONT color=#008000 size=1>//populateTreeView();
</P></FONT><P><FONT color=#008000 size=1>//3- Now You can visit each and every node of this treeview whenever you require</P></FONT><FONT size=1><P>TVIEW t1 = </FONT><FONT color=#0000ff size=1>new</FONT><FONT size=1> TVIEW();</P><P>t1.TraverseTreeView(treeview);</P><P></FONT>



private void TraverseTreeView(TreeView tview)<BR>

//Create a TreeNode to hold the Parent Node
TreeNode temp = new TreeNode();

//Loop through the Parent Nodes
for(int k=0; k<tview.Nodes.Count; k++) 
//Store the Parent Node in temp
temp = tview.Nodes[k];

//Display the Text of the Parent Node i.e. temp

//Now Loop through each of the child nodes in this parent node i.e.temp
for (int i = 0; i < temp.Nodes.Count; i++)
visitChildNodes(temp.Nodes[i]); //send every child to the function for further traversal

<P><FONT color=#0000ff size=1>private</FONT><FONT size=1> </FONT><FONT color=#0000ff size=1>void</FONT><FONT size=1> visitChildNodes(TreeNode node)</FONT></P><PRE lang=cs>
{ //Display the Text of the node


//Loop Through this node and its childs recursively

for (int j = 0; j < node.Nodes.Count; j++)




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

Web Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCan you attach a sample project? Pin
mdunn621-Aug-06 6:32
membermdunn621-Aug-06 6:32 
AnswerRe: Can you attach a sample project? Pin
SULMAN SARWAR21-Aug-06 20:10
memberSULMAN SARWAR21-Aug-06 20:10 
QuestionWhat's this?? Pin
AxelM21-Mar-06 0:51
memberAxelM21-Mar-06 0:51 
GeneralMissing Pin
The_Myth20-Mar-06 23:55
memberThe_Myth20-Mar-06 23:55 
GeneralRe: Missing Pin
SALMAN SARWAR21-Mar-06 1:11
memberSALMAN SARWAR21-Mar-06 1:11 
GeneralRe: Missing Pin
The_Myth21-Mar-06 1:59
memberThe_Myth21-Mar-06 1:59 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 22 Aug 2006
Article Copyright 2006 by SULMAN SARWAR
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid