datagridview上的不稳定InvalidOperationException

问题描述 投票:2回答:2

我的Winform应用程序在根级别记录AppDomain.CurrentDomain.UnhandledException和Application.ThreadException,我得到了这个异常:

System.InvalidOperationException:由于对象的当前状态,操作无效。在System.Windows.Forms.Forms.Forg.Cn上的System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender,ListChangedEventArgs e)的System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)处于System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)at at System.ComponentModel.BindingList1.OnListChanged(ListChangedEventArgs e) at System.ComponentModel.BindingList1上System.ComponentModel.BindingList1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection1.Add(T item)的System.ComponentModel.BindingList1.AddNewCore() at System.ComponentModel.BindingList1.FireListChanged(ListChangedType类型,Int32索引)中的System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender,ListChangedEventArgs e)。 System.Windows.Forms.Fornd.RindNew()at System.Windows.Forms.Forms.DataGridView.DataGridViewDataConnection.AddNew()在System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded()的System.Windows.Forms.CurrencyManager.AddNew()处。 System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&dataGridViewCell,Int32 columnIndex,Int32 rowIndex,Boolean canCreateN系统中的System.Windows.Forms.DataGridView.ProcessDownKeyInternal(Keys keyData,Boolean&moved)的System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,Boolean setAnchorCellAddress,Boolean validateCurrentCell,Boolean throughMouseClick)中的ewRow,Boolean validationFailureOccurred)位于System.Windows.Forms.TextBoxBase.ProcessDialogKey的System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)的System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData)中的.Windows.Forms.DataGridView.ProcessEnterKey(Keys keyData) (键keyData)位于System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG&msg)的System.Windows.Forms.Control.PreProcessControlMessageInternal(控件目标,消息和消息)中的System.Windows.Forms.Control.PreProcessMessage(Message&msg) )

这是ex.ToString()的结果,它不返回我的应用程序的自定义代码,只返回内部的System.Windows.Forms方法。

在某些客户机器上不时会出现例外,我甚至无法自己重现它。

这种气味并不好,当我更改datagridview的数据源边界时,我的假设就出现了。但在这种情况下,我应该至少在异常堆栈中看到我的类,但这里什么都没有。

找到根本原因或调试的任何线索?

非常感谢

c# winforms .net-4.0 invalidoperationexception
2个回答
1
投票

如果您调查堆栈跟踪,您将看到问题的根源:客户正在尝试在网格上添加新记录,因此事件处理程序尝试将记录添加到数据源,这将导致另一个事件处理程序,尝试将记录添加到绑定列表,这会导致事件currencyManager_listChanged启动,由于对象的错误状态而失败。

您可以处置您的清单,也可以不取消订阅处置控制的事件。


1
投票

我遇到了完全相同的异常,它发生在用户删除多行(启用多选)之后,并包含选择中的最后一行。最后一行是添加新项目。一切正常,直到用户然后去编辑剩余的行,并发生异常。

当用户在选择中不包括最后一个/添加新项目行时,删除后一切正常。

我没有时间调查这种行为发生的确切原因,但是解决方法是在多选项时不允许添加新项目行,这可以通过IsNewRowDataGridViewRow属性检测,然后调用ClearSelection()如果此行在选择中,则在DataGridView上。

private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        if (row.IsNewRow)
        {
            dataGridView1.ClearSelection();
            return;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.