diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs index a56f85becd4..8991873527c 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs @@ -16701,7 +16701,11 @@ protected override void OnPaint(PaintEventArgs e) using GraphicsClipScope clipScope = new(g); g.SetClip(gridRect); PaintBackground(g, clipRect, gridRect); - PaintGrid(g, gridRect, clipRect, SingleVerticalBorderAdded, SingleHorizontalBorderAdded); + + if (CurrentCell is not null) + { + PaintGrid(g, gridRect, clipRect, SingleVerticalBorderAdded, SingleHorizontalBorderAdded); + } } PaintBorder(g, clipRect, _layout.ClientRectangle); diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs index cee95b9b3d1..1e093a1899e 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs @@ -839,7 +839,10 @@ protected override void OnKeyUp(KeyEventArgs e, int rowIndex) e.Handled = true; } - NotifyMSAAClient(ColumnIndex, rowIndex); + if (DataGridView is not null) + { + NotifyMSAAClient(ColumnIndex, rowIndex); + } } } @@ -967,9 +970,12 @@ private void NotifyUiaClient() private void NotifyMSAAClient(int columnIndex, int rowIndex) { - Debug.Assert(DataGridView is not null); - Debug.Assert((columnIndex >= 0) && (columnIndex < DataGridView.Columns.Count)); - Debug.Assert((rowIndex >= 0) && (rowIndex < DataGridView.Rows.Count)); + if (DataGridView is null || + columnIndex < 0 || columnIndex >= DataGridView.Columns.Count || + rowIndex < 0 || rowIndex >= DataGridView.Rows.Count) + { + return; + } int visibleRowIndex = DataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible, 0, rowIndex); int visibleColumnIndex = DataGridView.Columns.ColumnIndexToActualDisplayIndex(columnIndex, DataGridViewElementStates.Visible);