Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.56145%. Comparing base (2f7ddc4) to head (ecf5b7f).
Report is 3 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #13320         +/-   ##
===================================================
- Coverage   62.56333%   62.56145%   -0.00188%     
===================================================
  Files           1560        1560                 
  Lines         159675      159675                 
  Branches       14904       14904                 
===================================================
- Hits           99898       99895          -3     
- Misses         59008       59011          +3     
  Partials         769         769                 
Flag Coverage Δ
Debug 62.56145% <ø> (-0.00188%) ⬇️
integration 11.31811% <ø> (-0.00104%) ⬇️
production 40.73408% <ø> (-0.00313%) ⬇️
test 95.69057% <ø> (ø)
unit 38.13477% <ø> (-0.00415%) ⬇️

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

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

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

@@ -345,6 +345,7 @@ private const DataGridViewAutoSizeRowCriteriaInternal InvalidDataGridViewAutoSiz
private int _inBulkPaintCount;
private int _inBulkLayoutCount;
private int _inPerformLayoutCount;
private bool _isReleasingDataSource;
Copy link
Member

Choose a reason for hiding this comment

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

Can we add another bit flag to this field _dataGridViewOper instead of a new bool?

@Tanya-Solyanik Tanya-Solyanik added the waiting-author-feedback The team requires more information from the author label Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-DataGridView waiting-author-feedback The team requires more information from the author waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InvalidOperationException when dialog containing focused DataGridView is closed
2 participants