Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a null-check for "DataGridView" property before executing OnMouseUp in function OnMouseUpInternal #12701

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Jan 3, 2025

Fixes #12692

Root Cause

Proposed changes

  • Add a check if DataGridView is not null before executing OnMouseUp in function OnMouseUpInternal, because the Parent DataGridView might have been disconnected in the user event handler that is executed before this call, for example if the user clears the DataGridView row.

Customer Impact

  • Application crashes when the customer clicks on a DataGridView cell, where previously the content was selected.

Regression?

  • Yes, from NET8

Risk

  • Minimal

Screenshots

Before

Object reference not set to an instance of an object exception pops up after clicked the DataGridViewCell content when the function DataGridView1_CellContentClick contains content
DataGridView1.Rows.Clear();
DataGridView1.Rows.Add(1, 0, "ABCD");

Image

After

DataGridView Rows can be cleaned and re-added normally
AfterChange

Test methodology

  • Manually, added a unit test

Test environment(s)

  • .net 10.0.0-alpha.1.24631.2
Microsoft Reviewers: Open in CodeFlow

@LeafShi1 LeafShi1 requested a review from a team as a code owner January 3, 2025 03:07
Copy link

codecov bot commented Jan 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.03495%. Comparing base (9007f33) to head (5e2c2a7).
Report is 5 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #12701         +/-   ##
===================================================
+ Coverage   76.00638%   76.03495%   +0.02857%     
===================================================
  Files           3181        3181                 
  Lines         639670      639690         +20     
  Branches       47215       47215                 
===================================================
+ Hits          486190      486388        +198     
+ Misses        149968      149775        -193     
- Partials        3512        3527         +15     
Flag Coverage Δ
Debug 76.03495% <100.00000%> (+0.02857%) ⬆️
integration 18.16641% <0.00000%> (+0.12049%) ⬆️
production 49.82444% <100.00000%> (+0.05940%) ⬆️
test 97.03157% <100.00000%> (+0.00270%) ⬆️
unit 47.05064% <100.00000%> (-0.00035%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@LeafShi1 LeafShi1 added the waiting-review This item is waiting on review by one or more members of team label Jan 3, 2025
@ricardobossan
Copy link
Member

I tested the PR code, and the issue appears to be resolved:

12701

LGTM!

Copy link
Member

@Tanya-Solyanik Tanya-Solyanik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for jumping on this issue! Looks good, I added only minor comments.
Could you please also review the rootcause PR for similar cases, for example other instances of invocations of user-provided event handlers that are followed by an access to the parent DGV?

@@ -3040,7 +3040,7 @@ internal void OnMouseUpInternal(DataGridViewCellMouseEventArgs e)
DataGridView.OnCommonCellContentClick(e.ColumnIndex, e.RowIndex, e.Clicks > 1);
}

if (e.ColumnIndex < DataGridView.Columns.Count && e.RowIndex < DataGridView.Rows.Count)
if (DataGridView is not null && e.ColumnIndex < DataGridView.Columns.Count && e.RowIndex < DataGridView.Rows.Count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks confusing because at the top of this method we tested that DGV is not null already. Please add a comment explaining that the custom Click event handler might delete the cell and the parent DGV.


using DataGridView dataGridView = new();
dataGridView.Columns.Add(column);
SubDataGridViewCheckBoxCell cell = (SubDataGridViewCheckBoxCell)dataGridView.Rows[0].Cells[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:

Suggested change
SubDataGridViewCheckBoxCell cell = (SubDataGridViewCheckBoxCell)dataGridView.Rows[0].Cells[0];
var cell = (SubDataGridViewCheckBoxCell)dataGridView.Rows[0].Cells[0];

act.Should().NotThrow();
}

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
Copy link
Member

@Tanya-Solyanik Tanya-Solyanik Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please inline this event handler to make the test more case self-contained.

dataGridView.CellContentClick += new DataGridViewCellEventHandler((s, e) =>
{
DataGridView dataGridView = (DataGridView)s;
dataGridView.Rows.Clear();
dataGridView.Rows.Add();
}
);

@Tanya-Solyanik Tanya-Solyanik added 📭 waiting-author-feedback The team requires more information from the author and removed waiting-review This item is waiting on review by one or more members of team labels Jan 6, 2025
@Tanya-Solyanik Tanya-Solyanik changed the title Add judgment "DataGridView is not null" before executing OnMouseUp in function OnMouseUpInternal Add a null-check for "DataGridView" property before executing OnMouseUp in function OnMouseUpInternal Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📭 waiting-author-feedback The team requires more information from the author
Projects
None yet
3 participants