Click here to Skip to main content
Licence CPOL
First Posted 25 Mar 2008
Views 28,270
Downloads 593
Bookmarked 46 times

Loading a treeview to n levels

By | 25 Mar 2008 | Article
A recursive method to load a treeview to n levels

Introduction

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.

Background

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.

NodeIDParentIDNodeText
0-1Asia
1-1Africa
2-1North America
3-1South America
4-1Europe
5-1Australia
6-1Antarcatica
70South Asia
87Pakistan
98Federal Area
109West
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.

License

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

Member

Muhammad Usman
Software Developer, M.sc(Computer)
Pakistan

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
Questionthanks Pinmembercoolnavjot310:59 22 May '12  
GeneralMy vote of 5 Pinmemberme bill gates8:08 16 Dec '10  
Generalproblem in horizontal view Pinmemberskhurams20:46 5 Dec '10  
GeneralMy vote of 1 Pinmembervickybond1231:54 27 Dec '08  
GeneralRe: My vote of 1 Pinmembermdmahmudkhan19:17 8 Apr '12  
GeneralExcellent Pinmembererikdraven6:30 9 Sep '08  
Generalhello PinmemberAamir Mustafa3:24 28 Mar '08  
GeneralSimple and smart PinmemberRicardo borges16:01 26 Mar '08  
GeneralRe: Simple and smart PinmemberM Usman2:02 27 Mar '08  
GeneralGoood Article PinmemberAamir Mustafa2:47 26 Mar '08  
GeneralNice Article Pinmemberinetfly1237:22 25 Mar '08  

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
Web02 | 2.5.120517.1 | Last Updated 25 Mar 2008
Article Copyright 2008 by M Usman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid