32

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
priyanka
  • 543
  • 4
  • 10
  • 18

7 Answers7

34

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.

Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
  • I'm trying to delete a column if the user has changed it's value to null. Any idea how to do that? – David Nov 15 '12 at 00:05
  • @David Again, use the `CellValueChanged` event - but, personally, I would set the column's `Visible` property to false instead of actually deleting it. Less likely to cause problems that way. – Bradley Smith Nov 15 '12 at 00:38
33

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 ));
CouncilScribe
  • 691
  • 7
  • 19
  • Did it for me as well :-) – Roland Deschain Oct 22 '13 at 07:31
  • 1
    this worked as well, this is an actual solution rather then "make sure you aren't doing something", my problem was i was clearing the columns in the data grid and it didn't like that (since i was calling it as a result of CellEndEdit or something) – mgrandi Jan 24 '14 at 23:41
  • 1
    I was looking for a solution that was a bit cleaner than the `BeginInvoke` approach, but it turns out that Microsoft (UIFx Team) officially recommends this as the workaround. https://connect.microsoft.com/VisualStudio/feedback/details/390918/datagridview-control-throws-invalidoperationexception-reentrant-call-to-setcurrentcelladdresscore – Derek W Sep 15 '14 at 16:24
  • sadly, even the begininvoke method is breaking mine. I am calling the `.Columns.Clear();` to remove all of the custom columns i've added and unbinding the data (`DataSource=null`), before rebinding the data and re-adding the columns, and it generates the error whether or not I use the begininvoke. – MaxOvrdrv Feb 11 '16 at 18:08
  • BeginInvoke eliminated the SetCurrentCellAddressCore exception - however, the resulting code becomes non-deterministic, where your selected BindingSource.Position is no longer the same as the Invoked code's record. In my case, scrolling the grid executes Invoked code prior to my final scrolled to position and does not "catch up". – gridtrak Jun 21 '22 at 18:04
3

Set False to MultiSelect Property of your datagridview.

myDataGridView.MultiSelect=false;
Said Ait
  • 41
  • 1
2

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.

0

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

braX
  • 11,506
  • 5
  • 20
  • 33
Zonus
  • 2,313
  • 2
  • 26
  • 48
0

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();
clamchoda
  • 4,411
  • 2
  • 36
  • 74
-2

Putting an Application.DoEvents() in dataGridView.RowEnter can do it too.

zonko
  • 1
  • 1