繁体   English   中英

在C#中从Treeview获取数据

[英]Get the data from treeview in C#

说明:

编码:

       for (int i = 0; i < d.Count; i++) //loop for Product
            {
                TreeNode node = new TreeNode(((string[])d[i])[0]);
                thisForm.NewBKMTreeView.Nodes.Add(node); //add Product as Parent Node

                for (int j = 0; j < b.Count; j++) //loop for Item
                {
                    if (((string[])d[i])[1] == ((string[])b[j])[0]) //compare if ProductID from arrayList d same with ProductID from arrayList b
                    {
                        node.Nodes.Add(((string[])b[j])[2]); //add Item as Child Node
                    }
                }
            }

从上面的代码。

d是保存2个字符串的arraylist

string[0]       string[1]
ProductName     ProductID
-----------    -----------
  Food             001
  NotFood          002

b还有保存3个字符串的arraylist

string[0]      string[1]      string[2]
ProductID      itemID          itemName
  001            X101            Soup
  001            X102            Bread
  002            G111            Pen
  002            G212            Book
  002            G222            Ruler

将ProductName添加为父节点的代码:

TreeNode node = new TreeNode(((string[])d[i])[0]);

((((string [])d [i])[0])保留产品名称

将itemName添加为子节点的代码:

node.Nodes.Add(((string[])b[j])[2]);

((((string [])b [j])[2])持有itemName

运行完上面的编码后。 arrayList中的对象将显示在Treeview中

+Food
 - Soup
 - Bread
+NotFood
 - Pen
 - Book
 - Ruler

题 :

树视图是带有复选框的树视图。 因此用户可以检查他想要的项目。 并将项目复制到另一个地方。 我在这里有些问题。 用户在节点上检查时如何获取itemID?

我希望获得用于用户检查的项目的itemID,以从数据库中获取数据并将其复制到引用itemID的另一个位置。

我想你应该存储item id您从需要或任何其他信息item中树节点的标签属性。

您应该将数据库数据转换为对象:

public class MyNode
{
    public MyNode Parent { get; set; }
    public ObservableCollection<MyNode> Children { get; set; }
    public string ItemId { get; set; }
    // ...
}

如果要更改父级,只需为chilrends父级属性分配和新的父级即可。 更新父级和新父级的子级列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM