Tag: #datagridview invalidoperationexception

InvalidOperationException – 结束编辑单元格并移动到另一个单元格时

我制作了一个程序,我想手动更新数据网格视图。 – 我有一个通过清除它然后重新插入数据来刷新DGV的方法。 – 使用设计器,我为DGV的CellEndEdit创建了一个事件处理程序。 在事件处理程序内部,数据会更新并调用DGV的自定义刷新方法。 在运行程序时,每当我开始编辑单元格并通过选择另一个单元格结束它时,会抛出exception: InvalidOperationException操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用。 Visual C#的调试器标记清除数据的行:datagridview1.Rows.Clear(); 如果您想重现该问题,请使用visual c#创建一个新的Windows窗体项目,在窗体上放置一个DataGridView对象,并粘贴Form1.cs的以下代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Error___DataGridView_Updating___Cell_endedit { public partial class Form1 : Form { // Objects DataTable dt; DataColumn colID; DataColumn colName; DataColumn colInfo; // Constructor public Form1() { InitializeComponent(); Initialize_dt(); […]

如何逃避对setCurrentCellAddressCore的重入调用?

我有一个从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; […]