Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

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

$
0
0

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


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>