When binding a DataGridView
control to a binding source, I\'m getting the following error in my application:
Operation is not valid becau
Putting an Application.DoEvents() in dataGridView.RowEnter can do it too.
This can be caused by manipulating the datasource while the DataGridview
is in BeginEdit
.
Another solution is to SuspendBinding
on the CurrencyManager
of the DataGridView
while manipulating the datasource.
CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
currencyManager.SuspendBinding();
// Manipulate datasource
currencyManager.ResumeBinding();
The exception is raised by the DataGridView
in order to prevent an infinite loop from occurring. The cause of this is usually one of the following:
DataGridView
is still using itHave a look at your handler for the CellValueChanged
event and make sure you are not doing any of the above within the handler.
I found this exception happened because I had an empty DataGridView.CellValidated sub in my code. Once I deleted that empty sub the error went away.
This most likely caused by you attempting to refresh a DataGridView after a save. I suggest you invoke the method rather than just calling it.
BeginInvoke(new MethodInvoker(PopulateControl ));
This is very similar (and could be the same thing but without editing a cell). Anything that is done to a datagridview outside of the same thread that the control exists (event, background worker, another thread...) needs to be invoked. Read up on the solution here.
DataGridView InvalidOperationException reentrant call to SetCurrentCellAddressCore