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;
}
}
}
}