-
Notifications
You must be signed in to change notification settings - Fork 1k
Add _isReleasingDataSource to prevent unnecessary operations on CurrentCell when changing or releasing DataSource #13320
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
Conversation
…ntCell when changing or releasing DataSource
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
There was a problem hiding this 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. |
There was a problem hiding this 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?
src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.cs
Outdated
Show resolved
Hide resolved
…ead of field _isReleasingDataSource
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. |
/backport to release/8.0 |
/backport to release/9.0 |
/backport to release/9.0 |
/backport to release/8.0 |
Started backporting to release/8.0: https://github.com/dotnet/winforms/actions/runs/14607450996 |
/backport to release/9.0 |
Started backporting to release/9.0: https://github.com/dotnet/winforms/actions/runs/14607457364 |
Started backporting to release/8.0: https://github.com/dotnet/winforms/actions/runs/14607450996 |
@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! |
…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
@@ -1921,7 +1922,17 @@ public object? DataSource | |||
newDataSource.Disposed += OnDataSourceDisposed; | |||
} | |||
|
|||
CurrentCell = null; | |||
_dataGridViewOper[OperationInReleasingDataSource] = true; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
…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)
…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)
Fixes #13304
Root cause
Regression introduced in PR #4637
When closing the dialog in edit mode,
DataSource
is set to null, and thenCurrentCell = null
is set. Then unnecessary methodEndEdit
of theSetCurrentCellAddressCore
is called.Proposed changes
_isReleasingDataSource
in DataGridView and set it to null in propertyDataSource
before settingCurrentCell = null
to prevent theEndEdit
method of the SetCurrentCellAddressCore from being calledCustomer Impact
Regression?
Risk
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 thrownDataGridViewCrash.mp4
After
The DataGridView dialog with focus can be closed successfully
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow