简体   繁体   English

C# DataGridView 红色 X

[英]C# DataGridView Red X

I'm receiving a JSON file from a webserver, and then I use it to update a C# DataGridView control's DataSource property, but sometimes my DataGridView vanishes and is replaced by a big, red 'X'.我从网络服务器接收一个 JSON 文件,然后我用它来更新 C# DataGridView 控件的 DataSource 属性,但有时我的 DataGridView 消失并被一个大的红色“X”取代。 This is the .NET call stack that I get as a result:这是我得到的 .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)

What can I do to avoid this problem?我该怎么做才能避免这个问题?

Since your stack trace looks rather similar to mine, I'm going to suppose you're having a similar issue to what I had.由于您的堆栈跟踪看起来与我的非常相似,因此我假设您遇到了与我类似的问题。 If so, extending the DataGridView should solve your problem.如果是这样,扩展 DataGridView 应该可以解决您的问题。

Create a new User Control in your project (I called mine DataGridViewExt , so I'm going to use that name here), as such:在您的项目中创建一个新的用户控件(我称我的为DataGridViewExt ,因此我将在此处使用该名称),如下所示:

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

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

Notice you're extending DataGridView instead of UserControl .请注意,您正在扩展DataGridView而不是UserControl All you're doing here is adding an extra layer of error checking in the control itself, and telling the DGV to redraw itself when it runs into an error.您在这里所做的只是在控件本身中添加一个额外的错误检查层,并告诉 DGV 在遇到错误时重新绘制自己。 After this, add this control instead of each DataGridView for your project (if you've already added them, replace each DataGridView with your DataGridViewExt or whatever you decided to call it in the Designer).在此之后,为您的项目添加此控件而不是每个DataGridView (如果您已经添加了它们,请将每个DataGridView替换为您的DataGridViewExt或您决定在设计器中调用它的任何内容)。

You may run into an issue in the DataGridViewExt Designer where it adds handling for AutoScaleMode , which DataGridView does not have.您可能会在DataGridViewExt设计器中遇到一个问题,它添加了对AutoScaleMode处理,而DataGridView没有。 Remove this line, and you're good to go, no more big red X.去掉这条线,你就可以开始了,不再有大红X了。

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

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