0

After struggling with my first TreeView, it almost works. My problem is that in the Click Event, at the bottom of the code, the root node appears to be the only node in the collection, which is preventing me from checking all nodes when the root node is checked. I don't know what I did to cause this although I suspect I have probably added the nodes incorrectly. There is a root node and 12 parentNode nodes in the tree. Each parentNode has multiple secondChild nodes. Each secondChild node has multiple thirdChild nodes. This is a Windows Form. All of the code is listed here. Any help is appreciated.

    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;
               }
            }
        }
    }
dovid
  • 6,354
  • 3
  • 33
  • 73
mengli00
  • 11
  • 3

1 Answers1

0

The Nodes collection of the treeview only tells you how many nodes in this collection exists, you're adding the child nodes to other nodes, so to see how many nodes in total exists you must traverse the node tree.

If you want to check just the nodes inside the root node you can check with tvDiscountMaintenance.Nodes[0].Nodes

Gusman
  • 14,905
  • 2
  • 34
  • 50