【问题标题】:Treeview problems in WinForm C#WinForm C#中的Treeview问题
【发布时间】:2012-07-02 02:18:22
【问题描述】:

我在数据库 ItemList 中有一个包含 ProdutID 和 ItemID 的表。

ProductID    ItemID
  AAA         1001
  AAA         1002
  AAA         1003
  BBB         1201
  BBB         1293
  CCC         1040
  DDD         2011
  DDD         3203

我想像这样将所有这些数据添加到 Treeview:

+AAA
 - 1001
 - 1002
 - 1003
+BBB
 - 1201
 - 1293
+CCC
 - 1040
+DDD
 - 2011
 - 3203

请教我怎么做。谢谢。

编辑:

我尝试将长度为 2 的数据(ProductID,ItemID)的每个项目添加到 ArrayList 中

   for(int i=0;i<arrayList.count;i++)
    {
       TreeNode treeNode = new TreeNode(((string[])arrayList[i])[0]);    
       treeview1.Nodes.Add(treeNode);
}

【问题讨论】:

  • 您需要什么帮助?你试过什么?
  • @JonathonReinhart 目前我试图在 ArrayList 中添加所有项目。然后 for (int i= 0;i
  • 最好在提供更多信息时编辑您的问题。代码不在 cmets 中。
  • @MahmoudGamal 我们不再使用它,目的不是阻碍人们的问题。见这里:meta.stackexchange.com/questions/137795/stack-overflow-is

标签: c# winforms treeview


【解决方案1】:
TreeGridNode nodeParent = treeGridView1.Nodes.Add("AAA");

TreeGridNode nodeChild = nodeParent.Nodes.Add("1001"); 
TreeGridNode nodeChild = nodeParent.Nodes.Add("1002"); 
TreeGridNode nodeChild = nodeParent.Nodes.Add("1003"); 

您可以使用两个 for 循环动态地执行此操作

foreach (var product in Products)
{
  TreeGridNode nodeParent = treeGridView1.Nodes.Add(product.Id);
  foreach (var item in product.Items)
  {
   TreeGridNode nodeChild = nodeParent.Nodes.Add(item.Id); 
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 2021-10-04
    相关资源
    最近更新 更多