locked
Collection was modified; enumeration operation may not execute. ASP.Net Treeview Control RRS feed

  • Question

  • User-137565683 posted

    On a postback via a button control, I am trying to evaluate all of the CheckedNodes and set there childnode.Checked to true.  During the code below I receive the error:

    Collection was modified; enumeration operation may not execute. 

    Searching this error message has suggested to clone the checkednodes to a list, then update the children in the cloned list and set or reference the checknodes to this list to update it.  Seems like a lot of hoops to jump through, to accomplish a simple childnode update in a treeview.

    The other suggestion is not to use for each and replace it with a for() loop.

    Can someone suggest based on the code below, a method to update the "checked"  to true of the children of a checked Parent Node.... The code below works great through the first iteration, but after updating the first set of child nodes, it spawns the above error.

    THANKS!

           if (myTreeVew.CheckedNodes.Count > 0)
                {

                    foreach (TreeNode node in myTreeVew.CheckedNodes)
                    {
                        if (node.Checked == true)
                        {
                            foreach (TreeNode childnode in node.ChildNodes)
                            {
                                childnode.Checked = true;
                            }
                        }
                    }
                }

    Tuesday, April 12, 2016 12:44 PM

Answers

  • User-2057865890 posted

    Hi aspdev0978,

    The other suggestion is not to use for each and replace it with a for() loop.

    Yes. You could use for() loop.

    if (TreeView1.CheckedNodes.Count > 0)
    {
          for (int i = 0; i < TreeView1.CheckedNodes.Count; i++)
          {
                 if (TreeView1.CheckedNodes[i].Checked)
                 {
                       for (int j = 0; j < TreeView1.CheckedNodes[i].ChildNodes.Count; j++)
                       {
                             TreeView1.CheckedNodes[i].ChildNodes[j].Checked = true;
                       }
                 }
         }
    }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 14, 2016 7:37 AM

All replies

  • User-2057865890 posted

    Hi aspdev0978,

    The other suggestion is not to use for each and replace it with a for() loop.

    Yes. You could use for() loop.

    if (TreeView1.CheckedNodes.Count > 0)
    {
          for (int i = 0; i < TreeView1.CheckedNodes.Count; i++)
          {
                 if (TreeView1.CheckedNodes[i].Checked)
                 {
                       for (int j = 0; j < TreeView1.CheckedNodes[i].ChildNodes.Count; j++)
                       {
                             TreeView1.CheckedNodes[i].ChildNodes[j].Checked = true;
                       }
                 }
         }
    }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 14, 2016 7:37 AM
  • User-137565683 posted

    Thank you Chris...

    It is just interesting that the for next loop wouldn't allow you to update the collection.  But the for loop did.  Thanks to your reply I understand how to structure the for loop.

    Thanks again !

    Monday, April 18, 2016 12:14 AM