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

前端 未结 7 2181
旧时难觅i
旧时难觅i 2020-12-01 15:37

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

Operation is not valid becau

相关标签:
7条回答
  • 2020-12-01 16:14

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

    0 讨论(0)
  • 2020-12-01 16:18

    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();
    
    0 讨论(0)
  • 2020-12-01 16:20

    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.

    0 讨论(0)
  • 2020-12-01 16:22

    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 讨论(0)
  • 2020-12-01 16:26

    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 ));
    
    0 讨论(0)
  • 2020-12-01 16:26

    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

    0 讨论(0)
提交回复
热议问题