Why is my bound DataGridView throwing an "Operation not valid because it results in a reentrant call to the SetCurrentCellAddressCore function" error?

When binding a DataGridView control to a binding source, I'm getting the following error in my application:

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function

The binding source depends on the data table. And I'm filtering the records from the DataGridView. And I used the dataGridView1_CellValueChanged() event where I'm filtering the DataGridView. But when I was deleting the data from the current cell, this error occurs.

How can I resolve this problem?


Solution 1:

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:

  • Changing the active cell while an operation is being performed on the currently-active cell
  • Beginning, ending or cancelling edit mode while a cell edit is already in-progress
  • Any other operation that results in the active cell being changed while the DataGridView is still using it

Have a look at your handler for the CellValueChanged event and make sure you are not doing any of the above within the handler.

Solution 2:

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