Hi following code migth help you //Populate treeview at root level public void PopulateRootLevel( TreeView tv) { DataTable dt = new DataTable(); string sql = "SELECT MenuId,MenuName,MenuDescription,LinkName,(Select count(*) from menus where Parentid=mns.MenuId and menus.Status='Yes') as childnodecount,SecurityLevel FROM `menus` mns WHERE mns.Status = 'Yes' and mns.parentid is null"; try { connectToDB(); MySqlCommand cmdRoot = new MySqlCommand(sql, connection); MySqlDataAdapter da = new MySqlDataAdapter(cmdRoot); da.Fill(dt); } catch (Exception) { } finally { connection.Close(); connection.Dispose(); } PopulateNodes(dt, tv.Nodes); } //Populate nodes private void PopulateNodes(DataTable dt, TreeNodeCollection nodes) { foreach (DataRow dr in dt.Rows) { TreeNode tn = new TreeNode(); tn.Text = dr["MenuName"].ToString(); tn.Value = dr["MenuID"].ToString(); tn.ToolTip = dr["MenuDescription"].ToString(); tn.NavigateUrl = dr["LinkName"].ToString(); nodes.Add(tn); //If node has child nodes, then enable on-demand populating tn.PopulateOnDemand = int.Parse(dr["childnodecount"].ToString()) > 0; } }
Naveed Kamboh Complexity kills, Write easy code for your self. isolutionteam