Click here to Skip to main content
Licence CPOL
First Posted 21 Mar 2008
Views 34,459
Bookmarked 26 times

How to load data from database to TreeView

By | 21 Mar 2008 | Article
How to load data from database to TreeView

Introduction

How to load data from database into the Treeview control

1-Create two table as show below

Table ParentTable

Table1.jpg

ChildTable

Table2.jpg

2-Add some data

ParentTable

Table3.jpg

ChildTable

Table4.jpg

Drag and drap a TreeView control on the web page

As show below

Image1.jpg

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

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

{

fill_Tree2();

}
        

void fill_Tree2()

{



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

TreeView1.Nodes.Clear();

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.Expand();

tnParent.Selected = true;

TreeView1.Nodes.Add(tnParent);

FillChild(tnParent, tnParent.Value);

}

 

}

public void FillChild(TreeNode parent,string ParentId)

{

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

parent.ChildNodes.Clear();

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;

child.CollapseAll();

parent.ChildNodes.Add(child);

}

}

 

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();

ad.Fill(ds);

return ds;

 

}

Image2.jpg

Points of Interest

Learn new control

History

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

License

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

About the Author

T.Ashraf

Technical Lead
TANVTECH
United States United States

Member

More than 10 years of experience in design, architecture and development of various commercial objects oriented application using C# 3.5, ASP.net, relational database like SQL 2008, Oracle, MySQL and SharePoint. Experienced in client-server application development, data security management system, e- commerce, automation processes, claim payment systems, file system and tracking systems, also has very good exposure to the entire software development life cycle.
Tanvtech.com
http://tanvtech.com/Articles/MyFirstSSRSReport.aspx
http://tanvtech.com/Articles/MyFirstSSRSSubReport.aspx
http://tanvtech.com/Articles/MyFirstWcfArticle.aspx
http://tanvtech.com/Articles/MyFirstWcfClientArticle.aspx

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
Questiongetting treeview control items Pinmembersowjnaya2:52 21 Mar '12  
AnswerRe: getting treeview control items PinmemberT.Ashraf14:07 3 May '12  
GeneralMy vote of 5 Pinmemberamit sopan Roman0:52 27 Feb '12  
GeneralGetting error Pinmembermrkeivan22:46 9 May '10  
GeneralRe: Getting error PinmemberT.Ashraf0:20 10 May '10  
GeneralHelp Me! PinmemberSay Vanden22:14 25 May '09  
GeneralRe: Help Me! PinmemberT.Ashraf0:10 26 May '09  
GeneralRe: Help Me! PinmemberSay Vanden15:29 26 May '09  
GeneralRe: Help Me! PinmemberT.Ashraf7:36 11 Jan '10  
GeneralSorry, but I have to vote this down Pinmemberachimera14:12 22 Mar '08  
GeneralRe: Sorry, but I have to vote this down PinmemberT.Ashraf22:37 22 Mar '08  
GeneralTree-Like DataBase Table... PinmemberSergey Alexander Gynech7:34 21 Mar '08  
GeneralRe: Tree-Like DataBase Table... PinmemberT.Ashraf7:43 21 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
Web03 | 2.5.120517.1 | Last Updated 21 Mar 2008
Article Copyright 2008 by T.Ashraf
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid