hi every one..
i am binding data in a tree view using windows C# .. auctually i finished most of the code.. but i did't know how to proceed further .. i attached the code hear.. as well as it display the parent id.. insted of group_name ...
private void Form1_Load(object sender, EventArgs e)
{
PopulateRootLevel();
}
private void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection("Data Source=CDTECHPRO2\\SQLEXPRESS;Initial Catalog=Image;Integrated Security=True");
SqlCommand objCommand = new SqlCommand("select ParentId,GroupName,(select count(*) FROM tree WHERE SubGroup=sc.ParentId) childnodecount FROM tree sc where SubGroup IS NULL", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, treeView1.Nodes);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
int NodeIndex = 0;
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Name = dr["GroupName"].ToString();
tn.Text = dr["ParentId"].ToString();
nodes.Add(tn);
if ((int)dr["childnodecount"] > 0)
{
NodeIndex++;
}
else
{
NodeIndex = 0;
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
int s = int.Parse(e.Node.Text);
PopulateSubLevel(s, e.Node);
}
private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection("Data Source=CDTECHPRO2\\SQLEXPRESS;Initial Catalog=Image;Integrated Security=True");
SqlCommand objCommand = new SqlCommand("select ParentId,GroupName,(select count(*) FROM tree " + "WHERE SubGroup=sc.ParentId) childnodecount FROM tree sc where SubGroup=@SubGroup", objConn);
objCommand.Parameters.Add("@SubGroup", SqlDbType.Int).Value = parentid;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.Nodes);
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
int s = int.Parse(e.Node.Text);
PopulateSubLevel(s, e.Node);
}
the datadatse is
parentid groupname subgroup
11 tree1 Null
12 tree2 Null
13 node1 11
14 node2 12
15 newnode 13
any one can help me..
regards
jagatheesan.