How to evade reentrant call to setCurrentCellAddressCore?

前端 未结 2 499
执念已碎
执念已碎 2020-12-06 11:50

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

private void moveRowTo(DataGridView table, int oldIndex         


        
相关标签:
2条回答
  • 2020-12-06 12:22
    if (
        (datagridview.SelectedCells[0].RowIndex != datagridview.CurrentCell.RowIndex) ||
        (datagridview.SelectedCells[0].ColumnIndex!= datagridview.CurrentCell.ColumnIndex)
       ) { return; }
    
    0 讨论(0)
  • 2020-12-06 12:29

    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.

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