Powered By Blogger

Monday, October 26, 2009

fill treeview programmatically in asp.net

public void TreeViewPopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["name"].ToString();
//tn.Value = dr["idd"].ToString();
nodes.Add(tn);
//If node has child nodes, then enable on-demand populating
//tn.PopulateOnDemand = ((int)dr["childnodecount"] > 0);
}
}

public void TreeViewPopulateSubLevel(String parentid, TreeNode parentNode)
{
DB.DbConnect(con);
cmd = new SqlCommand("select IDD,NAM,(select count(*) FROM model WHERE parentid=sc.idd) childnodecount FROM model sc where parentid='" + parentid + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
TreeViewPopulateNodes(dt, parentNode.ChildNodes);
}

public void TreeViewPopulateRootLevel(string Fld, TreeView TreeView1)
{
cmd = new SqlCommand("select IDD,NAM,(select count(*) FROM model WHERE parentid=sc.IDD) childnodecount FROM model sc where parentid IS NULL and nam='" + Fld + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
TreeViewPopulateNodes(dt, TreeView1.Nodes);
}

No comments:

Post a Comment