How to evade reentrant call to setCurrentCellAddressCore?
Asked Answered
P

2

9

I have a function that is called from cell_endedit. It moves a dataGridViewRow inside a dataGridView:

private void moveRowTo(DataGridView table, int oldIndex, int newIndex)
{
    if (newIndex < oldIndex)
    {
        oldIndex += 1;
    }
    else if (newIndex == oldIndex)
    {
        return;
    }
    table.Rows.Insert(newIndex, 1);
    DataGridViewRow row = table.Rows[newIndex];
    DataGridViewCell cell0 = table.Rows[oldIndex].Cells[0];
    DataGridViewCell cell1 = table.Rows[oldIndex].Cells[1];
    row.Cells[0].Value = cell0.Value;
    row.Cells[1].Value = cell1.Value;
    table.Rows[oldIndex].Visible = false;
    table.Rows.RemoveAt(oldIndex);
    table.Rows[oldIndex].Selected = false;
    table.Rows[newIndex].Selected = true;
}

at row table.Rows.Insert(newIndex, 1) i get the following error:

Unhandled exception of type "System.InvalidOperationException" in System.Windows.Forms.dll

Additional data: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

It happens when I click another cell in process of editing current cell. How do i evade such error and have my row inserted correctly?

Pulmonary answered 23/10, 2014 at 7:9 Comment(2)
I get the same error in a DataGridView with ReadOnly = True and FullRowSelect mode (.NET 4.6.2 Windows Forms, VS2019)Anthracosilicosis
I got that error message because I did not set the EditMode of the dataGridView to 'EditProgrammatically'.Zaccaria
W
27

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.

Wives answered 23/10, 2014 at 12:11 Comment(0)
G
-1
if (
    (datagridview.SelectedCells[0].RowIndex != datagridview.CurrentCell.RowIndex) ||
    (datagridview.SelectedCells[0].ColumnIndex!= datagridview.CurrentCell.ColumnIndex)
   ) { return; }
Grimsby answered 27/5, 2017 at 8:19 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Revival

© 2022 - 2024 — McMap. All rights reserved.