简体   繁体   中英

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'. This is the .NET call stack that I get as a result:

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.

Create a new User Control in your project (I called mine DataGridViewExt , so I'm going to use that name here), as such:

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 . 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. 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).

You may run into an issue in the DataGridViewExt Designer where it adds handling for AutoScaleMode , which DataGridView does not have. Remove this line, and you're good to go, no more big red X.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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