Skip to content

Commit 1390aa6

Browse files
[release/9.0] InvalidOperationException when closing a dialog with focused 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)
1 parent 3c3762a commit 1390aa6

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/System.Windows.Forms/src/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27484,7 +27484,8 @@ protected virtual bool SetCurrentCellAddressCore(int columnIndex,
2748427484
{
2748527485
int oldCurrentCellX = _ptCurrentCell.X;
2748627486
int oldCurrentCellY = _ptCurrentCell.Y;
27487-
if (oldCurrentCellX >= 0
27487+
if (IsHandleCreated
27488+
&& oldCurrentCellX >= 0
2748827489
&& !_dataGridViewState1[State1_TemporarilyResetCurrentCell]
2748927490
&& !_dataGridViewOper[OperationInDispose])
2749027491
{

0 commit comments

Comments
 (0)