12,943,742 members (56,009 online)
alternative version

#### Stats

63.3K views
51 bookmarked
Posted 25 Mar 2008

, 25 Mar 2008 CPOL
 Rate this:
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.

 NodeID ParentID NodeText 0 -1 Asia 1 -1 Africa 2 -1 North America 3 -1 South America 4 -1 Europe 5 -1 Australia 6 -1 Antarcatica 7 0 South Asia 8 7 Pakistan 9 8 Federal Area 10 9 West 11 10 G 11 12 11 Sector 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.

## Share

 Software Developer Marriala Technologies Pakistan
Software Developer, M.sc(Computer)
Pakistan

## You may also be interested in...

 First Prev Next
 My vote of 5 Member 1023653327-Aug-13 17:28 Member 10236533 27-Aug-13 17:28
 Many Thanks for this gem... Member 1023653327-Aug-13 17:28 Member 10236533 27-Aug-13 17:28
 great solution dela030428-Dec-12 10:12 dela0304 28-Dec-12 10:12
 thanks, it helps me a lot, where the idea/solutions comes from?, what about copy rights?
 thanks coolnavjot3122-May-12 0:59 coolnavjot31 22-May-12 0:59
 My vote of 5 me bill gates16-Dec-10 8:08 me bill gates 16-Dec-10 8:08
 problem in horizontal view skhurams5-Dec-10 20:46 skhurams 5-Dec-10 20:46
 My vote of 1 vickybond12327-Dec-08 1:54 vickybond123 27-Dec-08 1:54
 Re: My vote of 1 mdmahmudkhan8-Apr-12 19:17 mdmahmudkhan 8-Apr-12 19:17
 Excellent erikdraven9-Sep-08 6:30 erikdraven 9-Sep-08 6:30
 hello Aamir Mustafa28-Mar-08 3:24 Aamir Mustafa 28-Mar-08 3:24
 Simple and smart Ricardo borges26-Mar-08 16:01 Ricardo borges 26-Mar-08 16:01
 Re: Simple and smart M Usman27-Mar-08 2:02 M Usman 27-Mar-08 2:02
 Goood Article Aamir Mustafa26-Mar-08 2:47 Aamir Mustafa 26-Mar-08 2:47
 Nice Article inetfly12325-Mar-08 7:22 inetfly123 25-Mar-08 7:22
 Last Visit: 31-Dec-99 18:00     Last Update: 22-May-17 18:23 Refresh 1