本文介绍了DataGridView ComputeVisibleColumn NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时当我对datagridview进行更改时 - 例如删除列,或重命名列标题,或者接受基础数据表上的更改,我得到了空引用异常。



堆栈跟踪在System.Windows.Forms.DataGridView.ComputeVisibleColumns结束并从System开始.Windows.Forms.NativeWindowCallback。



我想尽可能防止这种情况发生或以某种方式捕获它。



我尝试创建一个继承自datagridview的datagridview类,并使用a覆盖跟踪中的一些函数尝试catch捕获异常,但它仍然将其抛出_COMPlusException,或者如果没有地方可以捕获异常,那么它会抛出错误并导致应用程序崩溃。



我不知道该怎么做。



我确实看到这被报告为微软的一个错误但无法复制。我真的很感激有关如何处理这个的任何想法。



System.NullReferenceException:对象引用未设置为对象的实例。 at System.Windows.Forms.DataGridView.LayoutScrollBars()
at System.Windows.Forms.DataGridView.ComputeLayout()
at System。 Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut,Boolean computeVisibleRows,Boolean invalidInAdjustFillingColumns,Boolean repositionEditingControl)
System.Windows.Forms.DataGridView.OnBandThicknessChanged的System.Windows.Forms.DataGridView.OnColumnWidthChanged(DataGridViewColumnEventArgs e)
(DataGridViewBand dataGridViewBand)
在System.Windows.Forms.DataGridView.AutoResizeColumnInternal(Int32 columnIndex,DataGridViewAutoSizeColumnCriteriaInternal autoSizeColumnCriteriaInternal,Boolean fixedHeight)
at System.Windows.Forms.DataGridView.AutoResizeAllVisibleColumnsInte rnal(DataGridViewAutoSizeColumnCriteriaInternal autoSizeColumnCriteriaFilter,Boolean fixedHeight)
System.Windows.Forms.DataGridView.OnAddedRow_PostNotification(Int32 rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca,Int32 rowIndex,Int32 rowCount,DataGridViewRow dataGridViewRow ,布尔值changeIsDeletion,Boolean changeIsInsertion,Boolean recreateNewRow,Point newCurrentCell)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e,Int32 rowIndex,Int32 rowCount)
at System.Windows.Forms.DataGridViewRowCollection.AddInternal( DataGridViewRow dataGridViewRow)
System.Windows.Forms.Forms.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
System.Windows.Forms.DataGridView的System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView) .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 System.Data.Data.OnListChanged(ListChangedEventArgs e)
System.Data.DataView.UpdateIndex(Boolean force,Boolean fireEvent)
System.Data.Data.UpdateIndex(Boolean force)
System.Data .DataView.SetIndex2(String newSort,DataViewRowState newRowStates,DataExpression newRowFilter,Boolean fireEvent)
System.Data.DataView.SetIndex(String newSort,DataViewRowState newRowStates,DataExpression newRowFilter)
at System.Data.DataView.set_Sort(字符串值)
System.Data.DataView.System.ComponentModel.IBindingList.ApplySort(PropertyDescriptor属性,ListSortDirection方向)

System.Windows.Forms.DataGridView.DataGridViewDataConnection.Sort(DataGridViewColumn dataGridVie) wColumn,ListSortDirection方向)
在System.Windows.Forms.DataGridView.SortInternal(IComparer比较器,DataGridViewColumn dataGridViewColumn,ListSortDirection方向)
在System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn,ListSortDirection方向)
System.Windows.Forms.DataGridView.OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
在System.Windows.Forms.Forms.Control.WmMouseUp的System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
中。 m,MouseButtons按钮,Int32单击)
在System.Windows.Forms.Control.WndProc(Message& m)

System.Windows.Forms.DataGridView.WndProc(Message& m)
在System。 Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd ,Int32 msg,IntPtr wparam,IntPtr lparam)

解决方案

Sometimes when I make a change to the datagridview - such as delete a column, or rename a column header, or acceptchanges on the underlying data table I get a Null Reference exception.

 

The stack trace ends at System.Windows.Forms.DataGridView.ComputeVisibleColumns and starts at System.Windows.Forms.NativeWindowCallback.

 

I want to prevent this from happening if possible or catch it somehow.

 

I tried creating a datagridview class that inherits from the datagridview and override some of the functions in the trace with a try catch to catch the exception but it still throws it up as  _COMPlusException or if there is no place to catch the exception then it throws an error and crashes the application.

 

I don't know what to do about this. 

 

I did see that this was reported as a bug to Microsoft but it could not be reproduced.  I would really appreciate any ideas about how to deal with this.

 

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataGridView.ComputeVisibleColumns()
   at System.Windows.Forms.DataGridView.LayoutScrollBars()
   at System.Windows.Forms.DataGridView.ComputeLayout()
   at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
   at System.Windows.Forms.DataGridView.OnColumnWidthChanged(DataGridViewColumnEventArgs e)
   at System.Windows.Forms.DataGridView.OnBandThicknessChanged(DataGridViewBand dataGridViewBand)
   at System.Windows.Forms.DataGridView.AutoResizeColumnInternal(Int32 columnIndex, DataGridViewAutoSizeColumnCriteriaInternal autoSizeColumnCriteriaInternal, Boolean fixedHeight)
   at System.Windows.Forms.DataGridView.AutoResizeAllVisibleColumnsInternal(DataGridViewAutoSizeColumnCriteriaInternal autoSizeColumnCriteriaFilter, Boolean fixedHeight)
   at System.Windows.Forms.DataGridView.OnAddedRow_PostNotification(Int32 rowIndex)
   at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
   at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount)
   at System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow dataGridViewRow)
   at System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
   at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
   at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
   at System.Data.DataView.OnListChanged(ListChangedEventArgs e)
   at System.Data.DataView.UpdateIndex(Boolean force, Boolean fireEvent)
   at System.Data.DataView.UpdateIndex(Boolean force)
   at System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, DataExpression newRowFilter, Boolean fireEvent)
   at System.Data.DataView.SetIndex(String newSort, DataViewRowState newRowStates, DataExpression newRowFilter)
   at System.Data.DataView.set_Sort(String value)
   at System.Data.DataView.System.ComponentModel.IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.SortInternal(IComparer comparer, DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction)
   at System.Windows.Forms.DataGridView.OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnMouseClick(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

解决方案


这篇关于DataGridView ComputeVisibleColumn NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:16