繁体   English   中英

C# DataGridView 红色 X

[英]C# DataGridView Red X

我从网络服务器接收一个 JSON 文件,然后我用它来更新 C# DataGridView 控件的 DataSource 属性,但有时我的 DataGridView 消失并被一个大的红色“X”取代。 这是我得到的 .NET 调用堆栈:

System.NullReferenceException: Object reference not set to an instance of an object.

System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
System.Windows.Forms.Control.WmPaint(Message& m)
System.Windows.Forms.Control.WndProc(Message& m)
System.Windows.Forms.DataGridView.WndProc(Message& m)
System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我该怎么做才能避免这个问题?

由于您的堆栈跟踪看起来与我的非常相似,因此我假设您遇到了与我类似的问题。 如果是这样,扩展 DataGridView 应该可以解决您的问题。

在您的项目中创建一个新的用户控件(我称我的为DataGridViewExt ,因此我将在此处使用该名称),如下所示:

public partial class DataGridViewExt : DataGridView
{
    public DataGridViewExt()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        try
        {
            base.OnPaint(e);
        }
        catch (Exception)
        {
            this.Invalidate();
        }
    }
}

请注意,您正在扩展DataGridView而不是UserControl 您在这里所做的只是在控件本身中添加一个额外的错误检查层,并告诉 DGV 在遇到错误时重新绘制自己。 在此之后,为您的项目添加此控件而不是每个DataGridView (如果您已经添加了它们,请将每个DataGridView替换为您的DataGridViewExt或您决定在设计器中调用它的任何内容)。

您可能会在DataGridViewExt设计器中遇到一个问题,它添加了对AutoScaleMode处理,而DataGridView没有。 去掉这条线,你就可以开始了,不再有大红X了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM