我在树视图控件方面遇到问题。我对 C# 非常陌生,并且坚持如何将 ID 添加到相应的树节点。我不确定这行代码是否包含 ID,否则我应该包含什么。我想获取所选节点的 ID。我该如何进行,请告诉我。我的代码如下 -
提前致谢。
private void PopulateTreeView()
{
treeDepartments.Nodes.Clear();
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)
{
TreeNode tn = new TreeNode(dr["dpName"].ToString());
foreach (DataRow drChild in dr.GetChildRows("Dept_SubDept"))
{
tn.Nodes.Add(drChild["sdName"].ToString());
}
treeDepartments.Nodes.Add(tn);
}
}