c#动态更新datagridview绑定list 出现大红叉

开发环境vs2022 .net6.0

初始化时的代码

  if (标本.GetDaiJianBiaoBen(15))
            {
                listbbsing = 标本.bbsing;
                //  FormMain2.listbbsing = listbbsing;
                this.uiDataGridView1.AutoGenerateColumns = false;
                this.uiDataGridView1.DataSource = listbbsing;// 标本.GetBiaoBenDesc(10).Tables[0];
                
                uiDataGridView1.Columns[1].Frozen = true;
            }

动态更新的方法

  public void DisplayBiaoBenList()
        {
            lock (this)
            {
                try
                {
                    if (listbbsing is null) return;
                    if (listbbsing.Count == 0)
                    {
                        Loger loger = new Loger();
                        loger.WriteLog(@"listbbsing为空");
                        return;
                    }
                    if (uiDataGridView1 is null)
                        return;
                    this.uiDataGridView1.AutoGenerateColumns = false;
                    uiDataGridView1.DataSource = null;
                    uiDataGridView1.DataSource = listbbsing;
                  
                    uiDataGridView1.Refresh();
                }
                catch (Exception ex)
                {
                    Loger loger = new Loger();
                    loger.WriteLog(@"刷新待检测列表异常:" + ex.ToString());
                }
            }
        }

动态更新超过10次就会出现大红叉,对像没有实例化:

System.NullReferenceException: Object reference not set to an instance of an object. 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 Sunny.UI.UIDataGridView.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.DataGridView.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

解决方法 重写datagridview

 public class MYDataGridView : DataGridView
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {
                Invalidate();
            }
        }
    }

 this.uiDataGridView1 = new MYDataGridView ();

经过多次测试问题解决。

你可能感兴趣的:(c#,开发语言)