【问题标题】:childnodes are not in the TreeView Collection子节点不在 TreeView 集合中
【发布时间】:2017-01-26 22:11:35
【问题描述】:

在我的第一个 TreeView 苦苦挣扎之后,它几乎可以工作了。我的问题是,在代码底部的 Click 事件中,根节点似乎是集合中的唯一节点,这阻止了我在检查根节点时检查所有节点。我不知道我做了什么导致这种情况,尽管我怀疑我可能错误地添加了节点。 树中有一个根节点和 12 个 parentNode 节点。每个 parentNode 有多个 secondChild 节点。每个 secondChild 节点都有多个 thirdChild 节点。这是一个 Windows 窗体。此处列出了所有代码。任何帮助表示赞赏。

    public Form1()
    {
        InitializeComponent();

        FillTreeParentNodes();
    }

    public void FillTreeParentNodes()
    {
        connect.Open();

        DataTable dtGroups = new DataTable("FirstChildNodes");

        DataSet dsNodes = new DataSet();
        dsNodes.Tables.Add(dtGroups);

        SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorDesc", connect);
        daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        daAdapter.Fill(dsNodes, "FirstChildNodes");
        daAdapter.Dispose();

        tvDiscountMaintenance.Nodes.Clear();

        if (dsNodes.Tables[0].Rows.Count > 0)
        {
            TreeNode root = new TreeNode("Select All");
            tvDiscountMaintenance.Nodes.Add(root);

            foreach (DataRow row in dsNodes.Tables[0].Rows)
            {

                TreeNode parentNode = new TreeNode(row["DisColorDesc"].ToString());
                parentNode.Text = row["DisColorDesc"].ToString();
                root.Nodes.Add(parentNode);

                FillChildNodes(parentNode, root);
            }
        }
    }

      private void FillChildNodes(TreeNode parentNode, TreeNode root)
    {

        DataTable dtSecondGroup = new DataTable("SecondChildNodes");

        DataSet dsCNodes = new DataSet();
        dsCNodes.Tables.Add(dtSecondGroup);

        SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorCodes", connect);
        daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        daAdapter.Fill(dsCNodes, "SecondChildNodes");
        daAdapter.Dispose();

        if (dsCNodes.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow chRow in dsCNodes.Tables[0].Rows)
            {
                if (parentNode.Text.Equals(chRow["DisColorDesc"].ToString()))
                {
                    TreeNode secondChild = new TreeNode();
                    secondChild.Text = chRow["DisColorCode"].ToString();
                    parentNode.Nodes.Add(secondChild);
                    FillThirdChildNodes(secondChild, root);
                }
            }
        }

    }

    private void FillThirdChildNodes(TreeNode secondChild, TreeNode root)
    {
        DataTable dtThirdGroup = new DataTable("ThirdChildNodes");

        DataSet dsThNodes = new DataSet();
        dsThNodes.Tables.Add(dtThirdGroup);

        SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVCategories", connect);
        daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
        daAdapter.Fill(dsThNodes, "ThirdChildNodes");
        daAdapter.Dispose();

        if (dsThNodes.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow chRow in dsThNodes.Tables[0].Rows)
            {
                if (secondChild.Text.Equals(chRow["DisColorCode"].ToString()))
                {
                    TreeNode thirdChild = new TreeNode();
                    thirdChild.Text = chRow["DisCategoryDesc"].ToString;
                    secondChild.Nodes.Add(thirdChild);
                }
            }
        }
        connect.Close();
    }

    private void tvDiscountMaintenance_Click(object sender, EventArgs e)
    {
        if (tvDiscountMaintenance.Nodes.Count > 0) // I think this is telling me that the root node is the only one in the Collection.
        {
            if (tvDiscountMaintenance.TopNode.Checked)
            {
               foreach (TreeNode node in tvDiscountMaintenance.Nodes)
               {
                 node.ExpandAll();
                 node.Checked = true;
               }
            }
        }
    }

【问题讨论】:

  • 您需要从找到的每个节点开始遍历其所有节点。最好递归完成..
  • 谢谢。您是否知道我可以看到已完成的先前问题或示例?我在这里已经脱离了我的元素。
  • 删除第一条评论。我可以自己去找。对不起!
  • 没问题,这里..

标签: c# winforms treeview


【解决方案1】:

树视图的 Nodes 集合仅告诉您该集合中存在多少个节点,您正在将子节点添加到其他节点,因此要查看总共存在多少个节点,您必须遍历节点树。

如果您只想检查根节点内的节点,可以使用tvDiscountMaintenance.Nodes[0].Nodes 进行检查

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多