Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys, i am having a problem regarding a tree view control. I am very new to C# and stuck on how to add ID to the corresponding tree nodes. I am not sure whether this lines of code holds ID or if not then what should i include. I want to get the ID of the selected node. How do i proceed please let me know. My code is given below -

Thanks in advance.

private void PopulateTreeView()

            String strConn = "Server =server;Database =DB;Integrated Security = True;";
            SqlConnection conn = new SqlConnection(strConn);
            SqlDataAdapter da = new SqlDataAdapter("Select * from tSubDepartments", conn);
            SqlDataAdapter daCategories = new SqlDataAdapter("Select * from tDepartments", conn);
            da.Fill(ds, "tSubDepartments");
            daCategories.Fill(ds, "tDepartments");

            ds.Relations.Add("Dept_SubDept", ds.Tables["tDepartments"].Columns["dpCode"], ds.Tables["tSubDepartments"].Columns["dpCode"]);
            foreach (DataRow dr in ds.Tables["tDepartments"].Rows)
                int id = Convert.ToInt32(dr["dpCode"]);
                TreeNode tn = new TreeNode(dr["dpName"].ToString());
                foreach (DataRow drChild in dr.GetChildRows("Dept_SubDept"))
Updated 18-Mar-13 14:03pm
Sergey Alexandrovich Kryukov 18-Mar-13 19:31pm    
How a tree view can possibly be related to ADO.NET? Yes, you use ADO.NET, but tree view is related to UI, so tag UI library you use and focus your question on UI, otherwise there is nothing to discuss.
PIEBALDconsult 18-Mar-13 20:04pm    
Sergey Alexandrovich Kryukov 18-Mar-13 20:16pm    
I'm just curious: how did you know? Just educated guess, based on comparison of code with API?
PIEBALDconsult 18-Mar-13 21:50pm    
Oh, yeah, sure.

1 solution

I tend to put the DataRow in the Tag property for later access.
Share this answer
Sergey Alexandrovich Kryukov 18-Mar-13 23:51pm    
Makes sense, a 5. There are other approaches...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900