Click here to Skip to main content
13,199,318 members (50,587 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 25 Mar 2008

Loading a treeview to n levels

, 25 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A recursive method to load a treeview to n levels


Treeviews are comonly used in many websites where different categories are required to display. A Tree structure may have n levels. This is why, a suitable piece of code may be required, that enables the binding upto n levels. Another example may be a directory structure like we have in windows.


This chunk of code is efficient in a sense that it uses recursion. I have seen some snippets of code around internet where they include one or more columns in the database to correctly populate the treeview.

We define a recursive method as a method that calls it self until a certain condition becomes true. For example:

The code above calculates the factorial of a number for example: factorial of 3 is 3 * 2 * 1. More information on recursion can be found here : Recursion

Using the code

Categories are generally designed in a unique manner, where one category has exactly one parent and one or more children. Thus we design our table with three columns that is NodeID, ParentID and NodeText where 'Node' stands for a category. '-1' can be choosen act as a ParentID for the root level Nodes. All other nodes having ParentID other than '-1' will exist under Root Level Nodes under their specific Parent Nodes.

2-1North America
3-1South America
70South Asia
98Federal Area
1110G 11
1211Sector 1

Here is the code that I am using to populate the tree.

It includes a recusive function, that adds the nodes to n levels. The function GetData() is simply returning the child nodes of a parent node. You can use it like a select query or you can read all categories in the database at once and can make this function iterate on rows as I am doing for this sample code with ReadData().

The above code will produce the following treeview:

Points of Interest

One can also use XML to store the data. In that case data will be read in a dataset or using a parsing mechanism depending upon the requirements. Remember that we have post back properties for the treeview control, so you will be able to navigate user to the required pages when a particular node is clicked.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

M Usman
Software Developer Marriala Technologies
Pakistan Pakistan
Muhammad Usman
Software Developer,

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1023653327-Aug-13 17:28
memberMember 1023653327-Aug-13 17:28 
QuestionMany Thanks for this gem... Pin
Member 1023653327-Aug-13 17:28
memberMember 1023653327-Aug-13 17:28 
Questiongreat solution Pin
dela030428-Dec-12 10:12
memberdela030428-Dec-12 10:12 
Questionthanks Pin
coolnavjot3122-May-12 0:59
membercoolnavjot3122-May-12 0:59 
GeneralMy vote of 5 Pin
me bill gates16-Dec-10 8:08
memberme bill gates16-Dec-10 8:08 
Generalproblem in horizontal view Pin
skhurams5-Dec-10 20:46
memberskhurams5-Dec-10 20:46 
GeneralMy vote of 1 Pin
vickybond12327-Dec-08 1:54
membervickybond12327-Dec-08 1:54 
GeneralRe: My vote of 1 Pin
mdmahmudkhan8-Apr-12 19:17
membermdmahmudkhan8-Apr-12 19:17 
GeneralExcellent Pin
erikdraven9-Sep-08 6:30
membererikdraven9-Sep-08 6:30 
Generalhello Pin
Aamir Mustafa28-Mar-08 3:24
memberAamir Mustafa28-Mar-08 3:24 
GeneralSimple and smart Pin
Ricardo borges26-Mar-08 16:01
memberRicardo borges26-Mar-08 16:01 
GeneralRe: Simple and smart Pin
M Usman27-Mar-08 2:02
memberM Usman27-Mar-08 2:02 
GeneralGoood Article Pin
Aamir Mustafa26-Mar-08 2:47
memberAamir Mustafa26-Mar-08 2:47 
GeneralNice Article Pin
inetfly12325-Mar-08 7:22
memberinetfly12325-Mar-08 7:22 

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.171020.1 | Last Updated 25 Mar 2008
Article Copyright 2008 by M Usman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid