如何查找节点级别 [英] How to find node level

查看:105
本文介绍了如何查找节点级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C#winform中的应用。

我在usercontrol中动态地从访问表中设置树视图中的节点。在此父节点中,部门和子节点是通过 DeptID 键的子部门。现在我想点击childnode时访问 Subdeptid 。另外,当我点击Parentnode时,我想访问 Deptid 。我试过,我得到了Node,但没有得到 Deptid subdeptid

怎么可能实现这个?





这里是代码

  //   on Load  
BALDeaprtment objbaldepat = new BALDeaprtment();
DataSet ds = new DataSet();
ds = objbaldepat.GetDepartmenttreeData(Globalclass.Activedatabase.ToString());
foreach (DataRow dr in ds.Tables [ 部门]。行)
{
TreeNode tn = new TreeNode(dr [ DeptName]。ToString());
foreach (DataRow drChild in dr.GetChildRows( Dept_sub))
{
tn.Nodes.Add(drChild [ SubDeptName]。ToString());
}
treeViewDept.Nodes.Add(tn);
}

/// 关于后续代码



我有节点,但我不能得到Deptid,Subdeptid我怎么能实现这个?...

解决方案

keep DeptId和树节点的Tag属性中的SubDeptId

  foreach (DataRow dr  in  ds.Tables [  Department]。行)
{
TreeNode tn = new TreeNode(dr [ DEPTNAME]的ToString());
tn.Tag = dr [ DeptId];
foreach (DataRow drChild in dr.GetChildRows( Dept_sub))
{
TreeNode tnChild = tn.Nodes.Add(drChild [ SubDeptName]。ToString());
tnChild.Tag = drChild [ SubDeptId];
}
treeViewDept.Nodes.Add(tn);
}





然后如果你知道treeViewDept.SelectedNode,你就可以得到id:

< pre lang =c#> int id =( int )treeViewDept.SelectedNode.Tag;


Alexander Sharykin的解决方案1很好,但它是半专业解决方案。



我建议你阅读它:如何在Visual C#中为TreeView节点创建Key属性 [ ^ ]。很高兴知道如何扩展课程;)



如需了解更多信息,请参阅:

类(C#) [ ^ ]

继承(C#) [ ^ ]

多态性(C#) [ ^ ]

< a href =http://msdn.microsoft.com/en-us/library/ms228387%28v=vs.90%29.aspx>继承和派生类 [ ^ ]

高级C#技术 [ ^ ]


我想告诉你如何轻松它是扩展TreeNode类,以便TreeView中的每个节点都可以包含您选择的即时可访问信息。你不需要对TreeView进行子类化。



我将首先定义一个名为'Information:

  public   class 信息
{
public string ID { get ; private set ; }
public string DeptName { get ; private set ; }
public string SubDeptName { get ; private set ; }

public 信息( string id, string deptname, string subdeptname)
{
ID = id;
DeptName = deptname;
SubDeptName = subdeptname;
}
}

为什么我们可以创建一个继承自TreeNode的类的类?选择是基于期望许多节点将共享公共部门名称和SubDeptNames,并且使用类意味着我们将指向同一个信息到多个节点的实例的指针...希望避免大量重复的字符串。 br />


自定义TreeNode类:

  public   TreeNodeEx:TreeNode 
{
public 信息NodeInformation { get < /跨度>; set ; }

public TreeNodeEx(信息信息)
{
NodeInformation = info;
this .Text = info.ID;
}
}

让我们看看我们如何创建自定义TreeNode类的一些实例:

 //方法中的某个位置... 

私有字符串id,dept,subdept;

id =营销;
dept =广告;
subdept =互联网广告;

//创建'信息类
的实例信息信息=新信息(id,dept,subdept);

//创建10个自定义TreeNodes
//将它们添加到TreeView
for(int i = 0; i< 10; i ++)
{
TreeNodeEx newNode = new TreeNodeEx(info);
treeView1.Nodes.Add(newNode);
}

如何在自定义TreeNode类中访问扩展信息:

  private   void  treeView1_AfterSelect( object  sender,TreeViewEventArgs e)
{
TreeNodeEx selNode = treeView1.SelectedNode as TreeNodeEx;

// 在此处设置断点并检查'selNode
}

您将注意到,要访问扩展信息,我们必须将treeView1.SelectedNode返回的引用强制转换为扩展自定义TreeNode类型。没有那个演员阵容就会出现错误。



将一个Class实例添加到自定义TreeNode的一个很好的功能是在Visual Studio中检查一个实例时在TreeNode中,您会看到弹出窗口中单独显示的班级名称信息,您可以轻松打开它以查看自定义值。



你'请注意这里假设您创建TreeNodes时,您手头上有填充信息类实例字段所需的所有辅助信息。


Our application in C# winform.
I set nodes in treeview in usercontrol dynamically from access table. In this Parent node is department and child node is subdepartment through DeptID key. Now I want to access Subdeptid when i click childnode. Also I want to access Deptid when i click Parentnode. I tried and i got Node but didn't get its Deptid or subdeptid.
How could achieve this ?


here is code

//on Load 
 BALDeaprtment objbaldepat=new BALDeaprtment();            
            DataSet ds = new DataSet();
            ds = objbaldepat.GetDepartmenttreeData(Globalclass.Activedatabase.ToString());
            foreach (DataRow dr in ds.Tables["Department"].Rows)
            {
                TreeNode tn = new TreeNode(dr["DeptName"].ToString());
                foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
                {
                    tn.Nodes.Add(drChild["SubDeptName"].ToString());
                }
                treeViewDept.Nodes.Add(tn);                
            }

///on afterselect code 


I got node but i cant get Deptid,Subdeptid how could i acheive this?...

解决方案

keep DeptId and SubDeptId in Tag property of tree nodes

foreach (DataRow dr in ds.Tables["Department"].Rows)
            {
                TreeNode tn = new TreeNode(dr["DeptName"].ToString());
                tn.Tag = dr["DeptId"];
                foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
                {
                    TreeNode tnChild = tn.Nodes.Add(drChild["SubDeptName"].ToString());
                    tnChild.Tag = drChild["SubDeptId"];
                }
                treeViewDept.Nodes.Add(tn);
            }



then if you know treeViewDept.SelectedNode, you can get id:

int id = (int)treeViewDept.SelectedNode.Tag;


Solution 1 by Alexander Sharykin is good, but it's semi-professional solution.

I would suggest you to read it: How to create a Key property for a TreeView node in Visual C#[^]. It's good to know how to extend class ;)

For further information, please see:
Classes (C#)[^]
Inheritance (C#)[^]
Polymorphism (C#)[^]
Inheritance and Derived Classes[^]
Advanced C# Techniques[^]


I'd like to show you how easy it is to extend the TreeNode Class so that each Node in your TreeView can contain instantly accessible information of your choice. You don't need to sub-class the TreeView.

I'll start by defining a Class named 'Information:

public class Information
{
    public string ID { get; private set; }
    public string DeptName { get; private set; }
    public string SubDeptName { get; private set; }

    public Information(string id, string deptname, string subdeptname)
    {
        ID = id;
        DeptName = deptname;
        SubDeptName = subdeptname;
    }
}

Why bother with a class, when we could just create a class that inherits from TreeNode in which you'd have fields ? The choice is based on the expectation that many nodes will share common Department Names and SubDeptNames, and using a Class means we stick a pointer to the same instance of 'Information into many Nodes ... hopefully that avoids a lot of duplicate strings.

The custom TreeNode Class:

public class TreeNodeEx : TreeNode
{
    public Information NodeInformation { get; set; }

    public TreeNodeEx(Information info)
    {
        NodeInformation = info;
        this.Text = info.ID;
    }
}

Let's look how we might create some instances of the custom TreeNode class:

// somewhere in a method ...

private string id, dept, subdept;

id = "Marketing";
dept = "Advertising";
subdept = "Internet Ads";

// create an instance of the 'Information class
Information info = new Information(id, dept, subdept);

// create 10 custom TreeNodes
// add them to the TreeView
for (int i = 0; i < 10; i++)
{
    TreeNodeEx newNode = new TreeNodeEx(info);
    treeView1.Nodes.Add(newNode);
}

How to access the extended information now in the custom TreeNode Class:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNodeEx selNode = treeView1.SelectedNode as TreeNodeEx;

    // set a break-point here and inspect the contents of 'selNode
}

You'll note that to access the extended information we had to cast the reference returned by treeView1.SelectedNode to our extended custom TreeNode Type. Without that cast there would be an error.

A nice feature of adding an instance of a Class to the custom TreeNode is that in Visual Studio when you inspect an instance of the TreeNode, you'll see the Class Name 'Information displayed separately in the pop-up, and you can open it easily to see the custom values.

You'll note the assumption made here that at the moment you create the TreeNodes you have, at hand, all the secondary information required to fill the fields of the instance of the 'Information class.


这篇关于如何查找节点级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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