Skip to content

Commit a29c04a

Browse files
authored
[release/8.0] InvalidOperationException when closing a dialog with focused 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)
2 parents 5b74dd6 + 209b149 commit a29c04a

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27460,7 +27460,8 @@ protected virtual bool SetCurrentCellAddressCore(int columnIndex,
2746027460
{
2746127461
int oldCurrentCellX = _ptCurrentCell.X;
2746227462
int oldCurrentCellY = _ptCurrentCell.Y;
27463-
if (oldCurrentCellX >= 0 &&
27463+
if (IsHandleCreated &&
27464+
oldCurrentCellX >= 0 &&
2746427465
!_dataGridViewState1[State1_TemporarilyResetCurrentCell] &&
2746527466
!_dataGridViewOper[OperationInDispose])
2746627467
{

0 commit comments

Comments
 (0)