![]() |
General Programming »
Algorithms & Recipes »
General
Intermediate
Bind Nested Tree view from databaseBy Ritesh SutariaBind Nested Tree View |
C#, Windows, .NET, Visual Studio, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This is an example of how to bind a nested tree from database.
Very Simple algorithm to bind tree.
method insertnodes is called recursively which will bind a nested tree at n level
create one table in access with following fields
module_id integer
module_name varchar(50)
parent_module integer
place one treeview control, and call this method with first parameter null
insertnodes(null,0);
private void insertnodes ( TreeNode n , int module_id )
{
OleDbConnection con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\tree.mdb");
con.Open();
OleDbCommand cmd=new OleDbCommand( "SELECT * FROM [module] WHERE parent_module=" + module_id,con);
OleDbDataReader rdr=cmd.ExecuteReader();
while(rdr.Read())
{
TreeNode t=new TreeNode(rdr["module_name"].ToString());
insertnodes(t,Convert.ToInt16(rdr["module_id"].ToString()));
if(n==null)
treeView1.Nodes.Add(t);
else
n.Nodes.Add(t);
}
rdr.Close();
}
source code is included which can be useful to understand the code
regards,
ritesh
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Aug 2006 Editor: |
Copyright 2006 by Ritesh Sutaria Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |