Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Here's the scenario:

I have to Forms , Form 1 and Form 2:

In Form 1, is where the DatagridView, in Form 2, all the textbox are set Modifiers to "Internal" so that i can show datas from datagrid.

What i am doing is I am showing datagridview to textbox to Form 2;

C#
LeaseShowDataFields lsdf = new LeaseShowDataFields();

            lsdf.metropropertycodetxtBox.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

            lsdf.Show();


It works correctly it shows to Form2, but after closing the Form 2 it throws error:
C#
Collection was modified; enumeration operation may not execute.

How to handle this Exception? Kindly Help
Posted

1 solution

You can't modify a collection while enumerating it, e.g. this would produce the same error:
C#
List<int> items = new List<int>() { 1, 2, 3 };
foreach(int item in items)
{
    items.Add(item+1);
}


You probably modify a collection from Form2 over which you are enumerating in Form1. Maybe the selected DataGrid-Rows? Maybe the DataGrid-Rows in general?

You can either make a copy of the collection you need to enumerate beforehand in order to modify the original collection while enumerating the copied one. Or you don't immediately modify the collection but create some kind of "actions to be taken on the collection"-list for when the enumeration is finished.
 
Share this answer
 
Comments
Rencyrence 31-Jan-16 21:28pm    
I can't understand, sorry, i didn't modify the collection, i just showing it on the textbox, is that already modifying the collection? Please tell me how to do it..
Sascha Lefèvre 31-Jan-16 23:52pm    
Please show more of your source code that could be relevant. Please do not post it as a comment here, instead click the "Improve question" link below your question and insert it there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900