Share via


Asp.net C#: Bind Tree View without recursive functions

Introduction

Below I have Posted the complete code to bind the tree view which is required for the tree view control to bind in ASP.NET C# using datasets.

Background

When I searched about tree view control in Google, I couldn't find much data  which shows how we can show the tree view control easily, so that made me want to write an article about how I got the output.

Tree view step by step explanation with example:

Steps

Step 1

Drag and drop the Tree view control from the Toolbox.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/Tree%20view.jpg

Step 2

Create a method which is used to generate the Tree view.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/Create%20a%20method.jpg 

The above marked code is used for creating the Tree Node Head and add to the Tree view Control.

Step 3

Now we are going to see how to effectively add the child nodes to the Parent Nodes (Which is mentioned above) to the correct position. For that first we need to get the nodes from the SQL server database, the code for that is:

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/SQL%20server%20database.jpg

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/root%20nodes.jpg

Once the dataset is returned, we can add the appropriate node to the tree view using the for loop as below:

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/loop.jpg

That's it,once you have the above code, you will get the output as follows:

Output

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/BlogImages/06072016132108PM/Output.jpg