簡體   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