当启用双缓冲时,我在使用C#.NET DataGridView进行双缓冲时遇到了一些问题。以前我的代码中没有问题,但我添加了以下行:
typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
这导致了良好的性能提升,并且已经在很多计算机上运行而没有任何问题。但是我收到了某人的消息,他在30或40分钟后收到以下错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.DataGridViewRowHeaderCell.PaintPrivate(Graphics
graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex,
DataGridViewElementStates dataGridViewElementState, Object formattedValue,
String errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts
paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds,
Boolean paint)
at System.Windows.Forms.DataGridViewRowHeaderCell.Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex,
DataGridViewElementStates cellState, Object value, Object formattedValue,
String errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts
paintParts)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex,
DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts
paintParts)
at System.Windows.Forms.DataGridViewRow.PaintHeader(Graphics graphics,
Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex,
DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean
isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle
clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates
rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle
boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle
gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean
singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)**strong text**
我已将以下内容添加到代码中,作为dataGridView的OnPaint事件的事件处理程序
private void dataGridView1_Paint(object sender, PaintEventArgs e)
{
try
{
base.OnPaint(e);
}
catch (Exception ex)
{
this.Invalidate();
}
}
我只能尝试看看它是否有效。 感谢