Skip to content

Conversation

LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Apr 17, 2025

Fixes #13304

Root cause

Regression introduced in PR #4637

When closing the dialog in edit mode, DataSource is set to null, and then CurrentCell = null is set. Then unnecessary method EndEdit of the SetCurrentCellAddressCore is called.

Proposed changes

  • Add _isReleasingDataSource in DataGridView and set it to null in property DataSource before setting CurrentCell = null to prevent the EndEdit method of the SetCurrentCellAddressCore from being called

Customer Impact

  • When the DataGridView is in editing mode, its dialog box can be closed normally

Regression?

  • Yes

Risk

  • Minimal

Screenshots

Before

When a dialog with a DataGridView that has focus is closed, an System.InvalidOperationException: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function. is thrown

DataGridViewCrash.mp4

After

The DataGridView dialog with focus can be closed successfully

AfterFix

Test methodology

  • Manually

Test environment(s)

  • .net 10.0.0-preview.4.25216.9
Microsoft Reviewers: Open in CodeFlow

…ntCell when changing or releasing DataSource
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.57350%. Comparing base (2f7ddc4) to head (59cbb81).
Report is 7 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #13320         +/-   ##
===================================================
+ Coverage   62.56333%   62.57350%   +0.01017%     
===================================================
  Files           1560        1560                 
  Lines         159675      159689         +14     
  Branches       14904       14906          +2     
===================================================
+ Hits           99898       99923         +25     
+ Misses         59008       58997         -11     
  Partials         769         769                 
Flag Coverage Δ
Debug 62.57350% <ø> (+0.01017%) ⬆️
integration 11.31854% <ø> (-0.00061%) ⬇️
production 40.75725% <ø> (+0.02004%) ⬆️
test 95.69057% <ø> (ø)
unit 38.15830% <ø> (+0.01937%) ⬆️

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@LeafShi1 LeafShi1 requested a review from Copilot April 18, 2025 00:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR prevents unnecessary operations on CurrentCell when changing or releasing DataSource by introducing a new flag.

  • Added a private flag (_isReleasingDataSource) to signal when DataSource is being released.
  • Wrapped the CurrentCell update in a try-finally block to maintain flag integrity.
  • Updated the SetCurrentCellAddressCore method to check the new flag and prevent calling EndEdit unnecessarily.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
DataGridView.cs Introduced _isReleasingDataSource and updated the DataSource setter to use a try-finally block when setting CurrentCell.
DataGridView.Methods.cs Modified the condition in SetCurrentCellAddressCore to skip EndEdit when _isReleasingDataSource is true.

@Tanya-Solyanik Tanya-Solyanik added the waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) label Apr 18, 2025
Copy link
Contributor

@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.

Added a small comment, please sent to testing when done. Is it possible to add a unit test to the UI tests assembly?

@Tanya-Solyanik Tanya-Solyanik added the waiting-author-feedback The team requires more information from the author label Apr 18, 2025
@LeafShi1
Copy link
Member Author

Added a small comment, please sent to testing when done. Is it possible to add a unit test to the UI tests assembly?

Currently this exception only occurs when the secondary form is closed. I have tried, but in unit test, it is not possible to open another form through an event on a main form page and perform operations on the secondary form.

@LeafShi1
Copy link
Member Author

LeafShi1 commented Apr 21, 2025

/backport to release/8.0

@LeafShi1
Copy link
Member Author

/backport to release/9.0

@Olina-Zhang
Copy link
Member

tested this PR change, GH issues: 13304, 4216 were fixed, no new issue found.

@Olina-Zhang Olina-Zhang removed the waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) label Apr 21, 2025
@Tanya-Solyanik Tanya-Solyanik merged commit 848bacb into dotnet:main Apr 21, 2025
8 checks passed
@Tanya-Solyanik
Copy link
Contributor

/backport to release/9.0

@dotnet-policy-service dotnet-policy-service bot added this to the 10.0 Preview4 milestone Apr 21, 2025
@LeafShi1
Copy link
Member Author

/backport to release/8.0

Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/winforms/actions/runs/14607450996

@LeafShi1
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/winforms/actions/runs/14607457364

Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/winforms/actions/runs/14607450996

Copy link
Contributor

@LeafShi1 backporting to release/8.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Add _isReleasingDataSource to prevent unnecessary operations on CurrentCell when changing or releasing DataSource
Using index info to reconstruct a base tree...
A	src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs
A	src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs
Falling back to patching base and 3-way merge...
Auto-merging src/System.Windows.Forms/src/System/Windows/Forms/DataGridView.cs
Auto-merging src/System.Windows.Forms/src/System/Windows/Forms/DataGridView.Methods.cs
CONFLICT (content): Merge conflict in src/System.Windows.Forms/src/System/Windows/Forms/DataGridView.Methods.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Add _isReleasingDataSource to prevent unnecessary operations on CurrentCell when changing or releasing DataSource
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

LeafShi1 added a commit to LeafShi1/winforms that referenced this pull request Apr 23, 2025
…ntCell when changing or releasing DataSource (dotnet#13320)

Fixes dotnet#13304

Root cause
Regression introduced in PR dotnet#4637

When closing the dialog in edit mode, DataSource is set to null, and then CurrentCell = null is set. Then unnecessary method EndEdit of the SetCurrentCellAddressCore is called.

Proposed changes
Add _isReleasingDataSource in DataGridView and set it to null in property DataSource before setting CurrentCell = null to prevent the EndEdit method of the SetCurrentCellAddressCore from being called
Customer Impact
When the DataGridView is in editing mode, its dialog box can be closed normally
Regression?
Yes
Risk
Minimal
@LeafShi1 LeafShi1 deleted the Issue_13304_fix_InvalidOperationException_DataGridView branch April 23, 2025 02:37
@@ -1921,7 +1922,17 @@ public object? DataSource
newDataSource.Disposed += OnDataSourceDisposed;
}

CurrentCell = null;
_dataGridViewOper[OperationInReleasingDataSource] = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we actually set this value inside the OnDataSourceDisposed instead of on any invocation of DataSource setter property? The new flag skips code that commits the value being editted, we want to skip the commit only if the control is diaposed and editing had not been completed.

Copy link
Member Author

@LeafShi1 LeafShi1 Apr 24, 2025

Choose a reason for hiding this comment

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

Yes, this makes more sense. CurrentCell = null; should also be placed in OnDataSourceDisposed, what do you think?

I submitted a new PR #13362

Copy link
Contributor

Choose a reason for hiding this comment

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

CurrentCell = null; should stay because it manages event handlers and that logic is needed when DataSource is changed for reasons other that disposal.

LeafShi1 added a commit that referenced this pull request May 8, 2025
…cused DataGridView control (#13350)

Backport of #13320 to release/8.0
Fixes #13304

## Proposed changes
- When the DataGridView is in edit mode and the parent form is being
closed, the DataGridView's `EndEdit` triggers the `CurrentCell` setting
logic, because the DGV's handle has been destroyed at this time, we are
re-creating it and then re-enter logic that sets up the current cell
causing an exception to be thrown. Checking `isHandleCreated` before
calling `EndEdit` can avoid executing related logic when the control has
been destroyed, thereby preventing exceptions from occurring.
 
 
## Customer Impact
- When the DataGridView is in editing mode, its dialog box can be closed
normally

## Regression?
- Yes, introduced in  #4637

## Testing
- Manual testing with the user-provided project

## Risk
- Low
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/13350)
LeafShi1 pushed a commit that referenced this pull request May 8, 2025
…cused DataGridView control (#13348)

Backport of #13320 to release/9.0
Fixes #13304

## Proposed changes
- When the DataGridView is in edit mode and the parent form is being
closed, the DataGridView's `EndEdit` triggers the `CurrentCell` setting
logic, because the DGV's handle has been destroyed at this time, we are
re-creating it and then re-enter logic that sets up the current cell
causing an exception to be thrown. Checking `isHandleCreated` before
calling `EndEdit` can avoid executing related logic when the control has
been destroyed, thereby preventing exceptions from occurring.
 
## Customer Impact
- When the DataGridView is in editing mode, its dialog box can be closed
normally

## Regression?
- Yes, introduced in  #4637

## Testing
- Manual testing with the user-provided project

## Risk
- Low

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/13348)
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-DataGridView waiting-author-feedback The team requires more information from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InvalidOperationException when dialog containing focused DataGridView is closed
3 participants