如何在树视图中添加ID [英] How to add ID in a tree view

查看:128
本文介绍了如何在树视图中添加ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对树视图控件有疑问。我是C#的新手,并坚持如何将ID添加到相应的树节点。我不确定这行代码是否包含ID,如果没有,那么我应该包括什么。我想获取所选节点的ID。我如何继续请告诉我。我的代码如下 -



提前致谢。



  private   void  PopulateTreeView()
{
treeDepartments.Nodes.Clear();

字符串 strConn = Server = server; Database = DB; Integrated Security = True;;
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter( 选择*来自tSubDepartments,conn);
SqlDataAdapter daCategories = new SqlDataAdapter( 选择*来自tDepartments,conn);
da.Fill(ds, tSubDepartments);
daCategories.Fill(ds, tDepartments);

ds.Relations.Add( Dept_SubDept,ds.Tables [ tDepartments]。列[ dpCode],ds.Tables [ tSubDepartments].Columns [ dpCode]);
foreach (DataRow dr in ds.Tables [ tDepartments]。行)
{
int id = Convert.ToInt32(dr [ dpCode]);
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);

}
}

解决方案

我倾向于将DataRow放在Tag属性中以供日后使用访问。

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()
        {
            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)
            {
                int id = Convert.ToInt32(dr["dpCode"]);
                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);
                
            }
        }

解决方案

I tend to put the DataRow in the Tag property for later access.


这篇关于如何在树视图中添加ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆