C#中的Treeview控件 [英] treeview control in c#

查看:262
本文介绍了C#中的Treeview控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Treeview控件的新手
我有表tree.mdb

表内容

I am new to treeview control
i have table tree.mdb

table contents

id    number
name  text
refid number



值是



values are

id      name           refid
_____________________________
1001     a             1000
1002     b             1001
1003     c             1002
1004     d             1003
1005     e             1001



我想在树状视图中将上面的表格显示为



i want display the above table in tree view as

1000
  |
  1001
    |
    1002
    | |
    | 1003
    |   |
    |   1004
    |
    1005



如何获得上面的树状视图

在此先感谢
Dineshkumar R



How can get the above tree view

thanks in advance
Dineshkumar R

推荐答案

可以使用其Nodes集合将根节点添加到TreeView.
The root nodes can be added to the TreeView using its Nodes collection.
TreeNode root1 = treeView1.Nodes.Add("Animal", "Animal");


同样,每个TreeNode都有一个Nodes集合.要将子节点添加到树中,请将新的TreeNode对象添加到适当的TreeNode的Nodes集合中.


Similarly, each TreeNode has a Nodes collection. To add sub nodes to the tree, add your new TreeNode object to the appropriate TreeNode''s Nodes collection.

TreeNode level1SubRoot1 = root1.Nodes.Add("Dogs", "Dogs");
TreeNode level2SubRoot1 = level1SubRoot1.Nodes.Add("GermanShepard", "German Shepard");
TreeNode level2SubRoot2 = level1SubRoot1.Nodes.Add("Labarador", "Labarador");



要从数据库填充TreeView,请在DataReader(ORDER BY ParentID ASC)中读取表,遍历阅读器,如果Parent ID为null,则将其添加到TreeView的Nodes集合中,否则使用TreeView.Nodes.Find()方法,然后将其添加到找到的节点的Nodes集合中.



To populate your TreeView from the database, read your table in a DataReader (ORDER BY ParentID ASC), loop through the reader, if the Parent ID is null, add it to the TreeView''s Nodes collection, otherwise find the TreeNode using the TreeView.Nodes.Find() method and then add it to the found node''s Nodes collection.

while dr.Read() {
    if (dr["refid"] == DBNull.Value) {
        treeView1.Nodes.Add(dr["id"].ToString(), dr["name"].ToString());
    } else {
        TreeNode node = treeView1.Nodes.Find(dr["refid"].ToString(), true);
        if (node != null) {
            node.Nodes.Add(dr["id"].ToString(), dr["name"].ToString());
        }
    }
}


注意:dr是已经打开的DataReader.


NOTE: dr is the DataReader that has already been opened.


假定您需要对此通用,即您的数据库可能包含其他结果,首先要做的是创建一个读取数据库并调用一个方法依次添加每个节点的框架.的有线版本为:
Assuming that you need to make this generic, i.e. that your database may contain other results, the first thing to do is to create a framework that reads the database and calls a method to add each node in turn. The hardwired version of this is:
AddNode(myTreeView, 1001, "a", 1000);
AddNode(myTreeView, 1002, "b", 1001);
AddNode(myTreeView, 1003, "c", 1002);
AddNode(myTreeView, 1004, "d", 1003);
AddNode(myTreeView, 1005, "e", 1001);

因此,您需要编写AddNode方法.但是首先,您将如何存储它? TreeNode只能存储文本和Tag值.我建议您首先创建一个类来保存节点信息,然后将其放入TreeNode标记中:

So, you need to write the AddNode method. But first, how are you going to store this? A TreeNode can only store text, and a Tag value. I would suggest that you first create a class to hold your node information, and put that into your TreeNode Tag:

public class myNode
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public myNode(int id, string name)
        {
        ID = id;
        Name = name;
        }
    }

简洁明了,但可以扩展.
现在,AddNode很简单,但是由于这是您的家庭作业,因此我没有给您实际的代码!
1)创建一个新的myNode实例.
2)创建一个新的TreeNode实例.将新的TreeNode.Tag属性设置为myNode实例.
3)找到您需要附加的父节点.
4)如果存在,则向其添加一个新节点.否则,将新节点添加到TreeView根目录.

找到父节点稍微复杂一点:您需要递归,所以创建一个新方法:

Nice and simple, but can be expanded.
Now, AddNode is simple, but since this is your homework, I''m not giving you the actual code!
1) Create a new myNode instance.
2) Create a new TreeNode instance. Set the new TreeNode.Tag property to the myNode instance
3) Find the parent node you need to attach to.
4) If it exists, add a new node to it. Otherwise, add the new node to the TreeView root.

Find the parent node is slightly more complex: you need recursion, so create a new method:

private TreeNode FindNode(TreeNodeCollection nodes, int id)


最初,您将使用TreeView的Nodes属性来调用它:


You will initially call this with the Nodes property of your TreeView:

TreeNode t = FindNode(treeView.Nodes, parentId);


FindNode也不难:
1)如果节点没有项目,则返回null-它不在此列表中.
2)遍历节点中的每个节点(请查看foreach循环)
2a)将node.Tag属性强制转换为myNode实例(在尝试使用myNode之前,最好先检查它是否为myNode).
2b)如果ID属性与您要查找的ID相匹配,请返回此节点.
2c)否则,请在此节点的node.Nodes属性上调用FindNode.
2d)如果找到该节点,则将其返回. (这会将找到的节点一直传递到链的后方)
2e)否则,请尝试列表中的下一个节点
3)如果节点用尽而未找到要查找的ID,则返回null.

听起来复杂吗?一点点.但是实际上,每个部分的代码都非常简单,实际上占用的空间少于上面的描述!

尝试-并不是很困难,如果遇到困难,请询问!


FindNode isn''t difficult either:
1) If nodes has no items, return null - it isn''t in this list.
2) Loop through each node in nodes (look at a foreach loop)
2a) Cast the node.Tag property to a myNode instance (it is a very good idea to check it is a myNode before you try to use it)
2b) If the ID property matched the ID you are looking for, return this node.
2c) Otherwise, call FindNode on the node.Nodes property of this node.
2d) If the node is found, return it. (this passes the found node all the way back up the chain)
2e) Otherwise, try the next node in the list
3) If you run out of nodes without finding the ID you are looking for, return null.

Sounds complex? A bit. But in practice, the code for each part is pretty simple, and actually takes less space that the description above!

Try it - it isn''t really difficult, and if you get stuck, ask!


这篇关于C#中的Treeview控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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