From 826374d63a246594a2e7a13ca064f64ba7d9a45a Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Wed, 22 Jan 2025 20:37:07 -0300 Subject: [PATCH] Prevents runtime crashes when the `DataGridView` or `CurrentCell` are null. Fixes #12752 ## Root Cause - The issue occurs because the `NotifyMSAAClient` method is called without checking if the `DataGridView` instance is `null`. This leads to a potential `NullReferenceException`. ## Proposed changes - Add a `null` check for the `DataGridView` instance before calling the `NotifyMSAAClient` method. - Adds another `null` check for the `CurrentCell` property before calling the `PaintGrid` method. ## Customer Impact - Prevents runtime crashes when the `DataGridView` or `CurrentCell` are null. ## Regression? - No ## Risk - Minimal ## Screenshots ### Before ### After ## Test methodology - Manual testing ## Test environment(s) - `10.0.100-alpha.1.25064.3` --- .../Forms/Controls/DataGridView/DataGridView.Methods.cs | 6 +++++- .../Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) 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..a9940ee6977 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); + } } }