Wednesday, February 11, 2009

Bind Nested Tree view from database - C#








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

1 comment:

quashabacke said...

Whether you wish to check out at|try} a slots game for free, check out a new new} blackjack strategy, or discover the most effective casinos to play roulette forreal money, you’ve come to the proper place. Some casino games have a ability element, the place the gamers' choices have an impact on the outcomes. Players possessing sufficient expertise to eliminate the inherent long-term disadvantage in a casino game are referred to as benefit gamers. If an web casino for actual cash 1xbet is secure and secure, they should to} readily provide security data to put their customer’s minds cozy.