Professional Documents
Culture Documents
New Microsoft Word Document
New Microsoft Word Document
Categories
Forums
Contact
Search
Follow @ASPSnippets
Here Mudassar Ahmed Khan has explained how to populate or bind TreeView from database in
ASP.Net using C# and VB.Net.
In order to populate TreeView with Parent Child relationship, I have made use of two tables
namely VehicleTypes and VehicleSubTypes. The schema and the data present in both the tables are
as follows.
VehicleTypes
VehicleSubTypes
Note: The
SQL for
creating the
database is
provided in the
attached sample
code.
HTML Markup
<h3>
Vehicle Details</h3>
<hr />
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
The PopulateTreeView method is a recursive function. Inside the Page Load event, the TreeView
is populated with the records from the VehicleTypes table.
Inside the PopulateTreeView method, a loop is executed over the DataTable and if the ParentId is
0 i.e. the node is a parent node, a query is executed over the VehicleSubTypes table to populate the
corresponding child nodes and again the PopulateTreeView method is called.
This process continues until all parent nodes along with their child nodes are added to the
TreeView.
C#
VB.Net
Demo
View Demo
Downloads
Download
Related Articles
Add Comments
You can add your comment about this article using the form below. Make sure you provide a valid email
address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are
Not relavant to the article
Spam
Advertising campaigns or links to other sites
Abusive content.
Please do not post code, scripts or snippets.
Follow @ASPSnippets
Name
Email
Comment
Security code:
Add Comment
Subscribe
Follow @ASPSnippets
Subscribe
What our readers say
Paul Wilcox
Just wanted to say I enjoyed this. Javascript is not my strongest language so its nice to have some
code to look at and play around with. I was able to get this up and running pretty quickly. Thanks.
Subscribe