I have a function that is called from cell_endedit. It moves a dataGridViewRow inside a dataGridView:
private void moveRowTo(DataGridView table, int oldIndex
if (
(datagridview.SelectedCells[0].RowIndex != datagridview.CurrentCell.RowIndex) ||
(datagridview.SelectedCells[0].ColumnIndex!= datagridview.CurrentCell.ColumnIndex)
) { return; }
This error is caused by
Any operation that results in the active cell being changed while the DataGridView is still using it
As the accepted answer in this post.
The fix (I have verified): use BeginInvoke to call moveRowTo.
private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
this.BeginInvoke(new MethodInvoker(() =>
{
moveRowTo(dataGridView2, 0, 1);
}));
}
BeginInvoke is an asynchronous call, so dataGridView2_CellEndEdit returns immediately, and the moveRowTo method is executed after that, at that time dataGridView2 is no longer using the currently active cell.