469,282 Members | 1,604 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

How can i add parent,child,sub child nodes on treeview in asp.net?

In the below code was executed succesful but it doesn't added child and subnodes plse send me code how can i add child nodes and subchild nodes in treeview aswellas in to database ,pleas help me
Expand|Select|Wrap|Line Numbers
  1. public void treeviewcontrol()
  2.     {
  3.         //con.Open();
  4.         TreeView1.Nodes.Clear();
  5.         SqlCommand cmd=new SqlCommand("select groups from treeview",con);
  6.         SqlDataAdapter da = new SqlDataAdapter(cmd);
  7.         DataSet ds = new DataSet();
  8.         da.Fill(ds);
  9.         con.Open();
  10.         foreach (DataRow dr in ds.Tables[0].Rows)
  11.         {
  12.             TreeNode group = new TreeNode();
  13.             group.Text = dr["groups"].ToString();
  14.             group.Value = dr["groups"].ToString();
  15.             //TreeView1.Nodes.Add(group);
  16.  
  17.             SqlCommand cmd1 = new SqlCommand("select subgroup from treeview where groups='" + group.Value+"'",con );
  18.             SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
  19.             DataSet ds1 = new DataSet();
  20.  
  21.             da1.Fill(ds1);
  22.  
  23.             foreach (DataRow dr1 in ds1.Tables[0].Rows)
  24.             {
  25.                 TreeNode subgroup = new TreeNode();
  26.                 subgroup.Text = dr1["subgroup"].ToString();
  27.                 subgroup.Value = dr1["subgroup"].ToString();
  28.                 group.ChildNodes.Add(subgroup);
  29.  
  30.                 SqlCommand cmd2 = new SqlCommand("select item from treeview where groups='" + group.Value+"'",con );
  31.                 SqlDataAdapter da2 = new SqlDataAdapter(cmd2 );
  32.                 DataSet ds2 = new DataSet();
  33.                 da2.Fill(ds2);
  34.                 foreach (DataRow dr2 in ds2.Tables[0].Rows) 
  35.                 {
  36.                     TreeNode item = new TreeNode();
  37.                     item.Text = dr2["item"].ToString();
  38.                     item.Value = dr2["item"].ToString();
  39.                   subgroup.ChildNodes.Add(item );
  40.                  item.CollapseAll();
  41.  
  42.                 } 
  43.             }
  44.             TreeView1.Nodes.Add(group);
  45.         }
  46.  
  47.  
  48.         con.Close();
  49.  
  50.         TreeView1.ExpandAll();
May 5 '11 #1
0 1272

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by =?Utf-8?B?ZGV0cml1czY3?= | last post: by
4 posts views Thread by jewel87 | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.