如何逃避对 setCurrentCellAddressCore 的重入调用? [英] How to evade reentrant call to setCurrentCellAddressCore?

查看:45
本文介绍了如何逃避对 setCurrentCellAddressCore 的重入调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 cell_endedit 调用的函数.它将 dataGridViewRow 移动到 dataGridView 中:

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;
}

在行 table.Rows.Insert(newIndex, 1) 我收到以下错误:

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

类型为System.InvalidOperationException"的未处理异常System.Windows.Forms.dll

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

附加数据:操作无效,因为它导致对 SetCurrentCellAddressCore 函数的可重入调用.

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?

推荐答案

这个错误是由

在 DataGridView 仍在使用它时导致活动单元格被更改的任何操作

Any operation that results in the active cell being changed while the DataGridView is still using it

作为接受的答案在这篇文章中.

修复(我已验证):使用BeginInvoke 调用moveRowTo.

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 是异步调用,所以dataGridView2_CellEndEdit 立即返回,之后执行moveRowTo 方法,此时dataGridView2 不再使用当前活动的单元格.

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.

这篇关于如何逃避对 setCurrentCellAddressCore 的重入调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆