如何规避折返调用setCurrentCellAddressCore? [英] How to evade reentrant call to setCurrentCellAddressCore?
问题描述
我有一个是从cell_endedit调用的函数。它移动一个的DataGridViewRow一个DataGridView里面:
私人无效moveRowTo(DataGridView的表,诠释oldIndex,诠释newIndex)
{
如果(newIndex< oldIndex)
{
oldIndex + = 1;
}
,否则如果(newIndex == oldIndex)
{
的回报;
}
table.Rows.Insert(newIndex,1);
的DataGridViewRow行= 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]。可见=虚假的;
table.Rows.RemoveAt(oldIndex);
table.Rows .Selected [oldIndex] = FALSE;
table.Rows .Selected [newIndex] = TRUE;
}
在排table.Rows.Insert(newIndex,1)我得到以下错误:
在
System.Windows.Forms.dll中键入System.InvalidOperationException的未处理的异常
其他数据:操作是无效的,因为它会导致重入调用SetCurrentCellAddressCore功能
块引用>
当我点击编辑当前单元格的过程中又发生了细胞。
我如何规避这样的错误,并有我行正确插入?
解决方案此错误是由
而在DataGridView仍然使用它被修改导致活动单元格中的任何操作。
块引用>
在本帖子接受的答案。
该修补程序(我已经验证):使用
的BeginInvoke
调用moveRowTo
。私人无效dataGridView2_CellEndEdit(对象发件人,DataGridViewCellEventArgs E)
{
this.BeginInvoke(新MethodInvoker( ()=>
{
moveRowTo(dataGridView2,0,1);
}));
}
为什么它的工作原理(在我看来):
的BeginInvoke
是一个异步调用,因此dataGridView2_CellEndEdit
立即返回,并经过执行moveRowTo
方法如此,在那个时候dataGridView2
不再使用当前活动单元格。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?
解决方案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 callmoveRowTo
.private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) { this.BeginInvoke(new MethodInvoker(() => { moveRowTo(dataGridView2, 0, 1); })); }
Why it works (In My Opinion):
BeginInvoke
is an asynchronous call, sodataGridView2_CellEndEdit
returns immediately, and themoveRowTo
method is executed after that, at that timedataGridView2
is no longer using the currently active cell.这篇关于如何规避折返调用setCurrentCellAddressCore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!