C#树视图列出子节点

时间:2013-12-02 18:36:07

标签: c# treeview nodes

我无法正确列出所有子节点。我有一个“员工”列表,里面有一个列表,用于检查硬件。 Employee名称和id显示得很好。我只是不能获得超过1个硬件检出显示。

这是我到目前为止所拥有的。

private void PopulateTreeView()
    {
        // sorts the fields by ascending order.
        frmCheckOut.checkedOutItem.SortList();

        // Clears the Tree View so there is no duplicate entries.
        tvInventory.Nodes.Clear();

        TreeNode date = new TreeNode();
        TreeNode[] nodeArray = new TreeNode[1];
        TreeNode itemDescription = new TreeNode();
        TreeNode[] nodeArray2 = new TreeNode[1];
        TreeNode treeNode = new TreeNode();

        foreach(CheckOutItem item in frmCheckOut.checkedOutItem.itemCollection)
       {
            foreach (Hardware h in item.hardware)
            {
                date = new TreeNode(h.Date);
                nodeArray = new TreeNode[] { date };
                itemDescription = new TreeNode(h.HName + ": " + h.TagNumber, nodeArray);
                nodeArray2 = new TreeNode[] { itemDescription };
            }

            treeNode = new TreeNode(item.EmpNumber + " " + item.LastName + ", " + item.FirstName + "<" + item.Email + ">", nodeArray2);

            tvInventory.Nodes.Add(treeNode);

        }
    }

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

更多地搞乱它之后。我想出了这个解决方案。它运作得很好。感谢所有评论家伙!

private void PopulateTreeView()
    {
        // sorts the fields by ascending order.
        frmCheckOut.checkedOutItem.SortList();

        // Clears the Tree View so there is no duplicate entries.
        tvInventory.Nodes.Clear();


        TreeNode treeNode = new TreeNode();

        foreach(CheckOutItem item in frmCheckOut.checkedOutItem.itemCollection)
       {
            treeNode = new TreeNode(item.EmpNumber + " " + item.LastName + ", " + item.FirstName + "<" + item.Email + ">");

            tvInventory.Nodes.Add(treeNode);
        }

        AddChildNodes();
    }

    private void AddChildNodes()
    {
        TreeNode date = new TreeNode();
        TreeNode[] nodeArray = new TreeNode[1];
        TreeNode itemDescription = new TreeNode();
        TreeNode[] nodeArray2 = new TreeNode[1];
        for (int i = 0; i < tvInventory.Nodes.Count; i++)
        {
            foreach (CheckOutItem item in frmCheckOut.checkedOutItem.itemCollection)
            {
                foreach (Hardware h in item.hardware)
                {
                    date = new TreeNode(h.Date);
                    nodeArray = new TreeNode[] { date };
                    itemDescription = new TreeNode(h.HName + ": " + h.TagNumber, nodeArray);
                    tvInventory.Nodes[i].Nodes.Add(itemDescription);
                }
            }
        }
    }