我有一个从cell_endedit调用的函数.它在dataGridView中移动dataGridViewRow:
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;
}
Run Code Online (Sandbox Code Playgroud)
在row table.Rows.Insert(newIndex,1)我收到以下错误:
System.Windows.Forms.dll中类型为"System.InvalidOperationException"的未处理异常
附加数据:操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用.
当我在编辑当前单元格时单击另一个单元格时会发生这种情况.如何规避此类错误并正确插入行?