Skip to content

Commit

Permalink
Prevents runtime crashes when the DataGridView or CurrentCell are…
Browse files Browse the repository at this point in the history
… null.

Fixes dotnet#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`
  • Loading branch information
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) committed Jan 22, 2025
1 parent 77ec5c9 commit 3d18116
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3d18116

Please sign in to comment.