【问题标题】:Erratic InvalidOperationException on datagridviewdatagridview 上的不稳定 InvalidOperationException
【发布时间】:2017-06-04 07:37:12
【问题描述】:

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

System.InvalidOperationException:由于对象的当前状态,操作无效。在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e) 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e) 在 System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e) 在System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e) at System.ComponentModel.BindingList1.OnListChanged(ListChangedEventArgs e) at System.ComponentModel.BindingList1.FireListChanged(ListChangedType type, Int32 index) at System.ComponentModel.BindingList1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection1.Add(T item ) 在 System.ComponentModel.BindingList1.AddNewCore() at System.ComponentModel.BindingList1.System.ComponentModel.IBindingList.AddNew() 在 System.Windows.Forms.CurrencyManager.AddNew() 在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() 在系统。 Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded() 在 System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canC reateNewRow, Boolean validationFailureOccurred) at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) 在 System.Windows.Forms.DataGridView.ProcessDownKeyInternal(Keys keyData, Boolean&moved) 在 System .Windows.Forms.DataGridView.ProcessEnterKey(Keys keyData) 在 System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData) 在 System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) 在 System.Windows.Forms.TextBoxBase.ProcessDialogKey (键 keyData)在 System.Windows.Forms.Control.PreProcessMessage(Message& msg) 在 System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) 在 System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg )

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

在某些客户机器上不时引发异常,我什至无法自己重现它。

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

有任何线索可以找到根本原因或进行调试吗?

非常感谢

【问题讨论】:

  • 当然,它并不完全指向您的代码,但它确实指向 System.Windows.Forms.DataGridView,从中可以引导您朝着您的 DataGridView 必须包含一些无效数据的方向前进。
  • 何时引发异常?您必须从客户那里获得一些信息。但是,我很确定这是相关的并且可能会有所帮助:stackoverflow.com/questions/2359124/…
  • 是的,它与我的 datagridview 数据有关,但很难找到,因为我的代码中没有显示异常...我已经在更新过程中添加了一个 try/catch all 但它什么也没捕获。 .
  • @Chris:是什么(操作)导致了这个异常?用户是否修改了 DataGridView 中的某些内容,例如单击了最后一行?
  • 好吧,因为我稍后正在查看日志,每次我问他们时,他们都无法描述这些操作......我正在考虑为客户添加一个对话框从他那里收集手动输入!而且我不明白为什么代码没有列在堆栈中,这对我来说是个谜

标签: c# winforms .net-4.0 invalidoperationexception


【解决方案1】:

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

要么释放列表,要么不取消订阅已释放控件的事件。

【讨论】:

    【解决方案2】:

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

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

    我还没有时间调查到底为什么会发生这种行为,但解决方法是在多选项目时不允许包含添加新项目行,这可以通过IsNewRow 的属性检测到DataGridViewRow,然后在 DataGridView 上调用 ClearSelection()(如果该行在选择中)。

    private void DataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            if (row.IsNewRow)
            {
                dataGridView1.ClearSelection();
                return;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多