Click here to Skip to main content
13,766,769 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 21 Mar 2008
Licenced CPOL

How to load data from database to TreeView

, 21 Mar 2008
Rate this:
Please Sign up or sign in to vote.
How to load data from database to TreeView


How to load data from database into the Treeview control

1-Create two table as show below

Table ParentTable




2-Add some data





Drag and drap a TreeView control on the web page

As show below


3-Copy the below listed code in the aspx.cs page

//Code Detail          
protected void Page_Load(object sender, EventArgs e)




void fill_Tree2()


DataSet PrSet = PDataset("Select * from ParentTable");


foreach (DataRow dr in PrSet.Tables[0].Rows)


TreeNode tnParent = new TreeNode();

tnParent.Text = dr["ParentName"].ToString();

tnParent.Value = dr["ParentID"].ToString(); 

tnParent.PopulateOnDemand = true;

tnParent.ToolTip = "Click to get Child";

tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;


tnParent.Selected = true;


FillChild(tnParent, tnParent.Value);




public void FillChild(TreeNode parent,string ParentId)


DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);


foreach (DataRow dr in ds.Tables[0].Rows)


TreeNode child = new TreeNode();

child.Text = dr["ChildName"].ToString().Trim();

child.Value = dr["ChildId"].ToString().Trim();

if (child.ChildNodes.Count == 0)


child.PopulateOnDemand = true;


child.ToolTip = "Click to get Child";

child.SelectAction = TreeNodeSelectAction.SelectExpand;






protected DataSet PDataset(string Select_Statement)


SqlConnection SqlCon = new SqlConnection("Data Source=;Integrated Security=True");

SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);

DataSet ds = new DataSet();


return ds;




Points of Interest

Learn new control


Keep a running update of any changes or improvements you've made here.


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


About the Author

Chief Technology Officer
United States United States
More than 15 years of experience in design, architecture and development of various commercial objects oriented application.Other Specialties

Data Migration:
• MS Access database to SQL 2005/2008
• MS Access database to Oracle
• MS Access database to My SQL
• FoxPro to SQL

Application Migration:
• Converted MS Access application to .net web application (
• Excel Application to .net 3.5 web application
• FoxPro application to .net 3.5
Reporting development and support
• MS access reports
• Crystal reports
• SQL Reports(SSRS)
• DevExpress reports
• Cognos reports
Application development and support
• .net Application web /Win forms
• SharePoint
• MS Access
• Website
• Ecommerce
• Web Services
3rd Party Control Support
• DevExpress
• .netForum
• Telerik
Version controls Support
• Team Foundation Server
• Source Safe

You may also be interested in...

Comments and Discussions

Questiongetting treeview control items Pin
sowjnaya21-Mar-12 3:52
membersowjnaya21-Mar-12 3:52 
AnswerRe: getting treeview control items Pin
T.Ashraf3-May-12 15:07
memberT.Ashraf3-May-12 15:07 
GeneralMy vote of 5 Pin
amit sopan Roman27-Feb-12 1:52
memberamit sopan Roman27-Feb-12 1:52 
GeneralGetting error Pin
mrkeivan9-May-10 23:46
membermrkeivan9-May-10 23:46 
GeneralRe: Getting error Pin
T.Ashraf10-May-10 1:20
memberT.Ashraf10-May-10 1:20 
GeneralHelp Me! Pin
Say Vanden25-May-09 23:14
memberSay Vanden25-May-09 23:14 
GeneralRe: Help Me! Pin
T.Ashraf26-May-09 1:10
memberT.Ashraf26-May-09 1:10 
GeneralRe: Help Me! Pin
Say Vanden26-May-09 16:29
memberSay Vanden26-May-09 16:29 
GeneralRe: Help Me! Pin
T.Ashraf11-Jan-10 8:36
memberT.Ashraf11-Jan-10 8:36 
GeneralSorry, but I have to vote this down Pin
achimera22-Mar-08 15:12
memberachimera22-Mar-08 15:12 
GeneralRe: Sorry, but I have to vote this down Pin
T.Ashraf22-Mar-08 23:37
memberT.Ashraf22-Mar-08 23:37 
GeneralTree-Like DataBase Table... Pin
Sergey Alexander Gynech21-Mar-08 8:34
memberSergey Alexander Gynech21-Mar-08 8:34 
GeneralRe: Tree-Like DataBase Table... Pin
T.Ashraf21-Mar-08 8:43
memberT.Ashraf21-Mar-08 8:43 

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 | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 21 Mar 2008
Article Copyright 2008 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid