Delete the selected parent node and child nodes - the collection has been changed; enumeration operation may not be performed

I have control over a tree with one level of parent and child nodes, each node has a checkbox for selecting nodes after selection, if I click the delete button. I want to remove the child or parent that has ever been selected, I use the following code and it returns an error

the code

protected void btnRemoveOrganisation_Click(object sender, EventArgs e) { foreach (TreeNode Item in tvwSelectedOrganisations.CheckedNodes) { if (Item.Parent == null) { foreach (TreeNode ChildNode in Item.ChildNodes) { Item.ChildNodes.Remove(ChildNode); } tvwSelectedOrganisations.Nodes.Remove(Item); } else { Item.Parent.ChildNodes.Remove(Item); } } } 

Error

The collection has been modified; enumeration operation cannot be performed.

Modified code

  protected void btnRemoveOrganisation_Click(object sender, EventArgs e) { TreeNodeCollection SelectedNodes = tvwSelectedOrganisations.CheckedNodes; foreach (TreeNode Item in SelectedNodes) { if (Item.Parent == null) { tvwSelectedOrganisations.Nodes.Remove(Item); } else { tvwSelectedOrganisations.FindNode(Item.Parent.ValuePath).ChildNodes.Remove(Item); } if (SelectedNodes.Count == 0) { break; } } } 

Decision

 int SelectedCount = SelectedNodes.Count; for (int i = SelectedCount - 1; i >= 0; i--) { if (tvwSelectedOrganisations.CheckedNodes[i].Parent == null) { int j = tvwSelectedOrganisations.CheckedNodes[i].ChildNodes.Count; tvwSelectedOrganisations.Nodes.Remove(tvwSelectedOrganisations.CheckedNodes[i]); i += j; } else { tvwSelectedOrganisations.FindNode(tvwSelectedOrganisations.CheckedNodes[i].Parent.ValuePath).ShowCheckBox = false; tvwSelectedOrganisations.FindNode(tvwSelectedOrganisations.CheckedNodes[i].Parent.ValuePath).ChildNodes.Remove(tvwSelectedOrganisations.CheckedNodes[i]); } } 
+6
source share
3 answers

Hope you are using .Net 3.5 or higher.

 foreach (TreeNode ChildNode in Item.ChildNodes.ToList()) { Item.ChildNodes.Remove(ChildNode); } 

EDIT

If Item.ChildNodes is not Enumerable . Try below.

 for( int i = Item.ChildNodes.Count - 1; i >= 0; i-- ) { Item.ChildNodes.Remove(ChildNode); } 

or

 while (Item.ChildNodes.Count > 0) { Item.ChildNodes.Remove(ChildNode); } 
+4
source

You cannot have this line inside foreach because you are modifying the collection that you are listing:

 tvwSelectedOrganisations.Nodes.Remove(Item); 

Instead, create a new list of items to delete, then go to that list and delete items outside (and after) your existing foreach .

+2
source

This will definitely be an exception. You should not use the delete or remove element in the collection, looping in one collection. Instead, copy the Item.ChildNodes collection to an empty new collection, try to loop through the original collection and delete the items you want in the new collection after the loop outside the loop area assigns the modified collection to the original again.

+1
source

Source: https://habr.com/ru/post/897230/


All Articles