
Introduction
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();
}
Regards,
Ritesh
No comments:
Post a Comment