You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
InvalidOperationException when dialog containing focused DataGridView is closed #13304
Crash repros with at least .NET 8.0.15 and .NET 10 preview 3
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
not sure which version introduced the issue, but there's no crash on .NET Framework
Issue description
When a dialog with a DataGridView that has focus is closed, an InvalidOperationException is thrown:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.InvalidOperationException: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
at System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)
Steps to reproduce
Use the attached project and follow the following steps (the attached video shows the steps as well):
Start the app
Open the dialog with the 'Open dialog' button
Focus one of the inputs on the new data grid row
Try to close the dialog using 'X'
An exception is thrown, while I would have expected the dialog to close gracefully.
DataGridViewSample.dll!DataGridViewSample.DataGridViewDialog.Dispose(bool disposing) Line 23 C# System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose(bool disposing) Line 67 C#
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.Dispose(bool disposing) Line 961 C#
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose() Line 61 C#
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose(bool disposing) Line 165 C#
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose() Line 149 C#
DataGridViewSample.dll!DataGridViewSample.DataGridViewDialog.Dispose(bool disposing) Line 21 C#
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose() Line 61 C#
Verified the issue with .NET SDK 8.0.15 + private dlls built from release/8.0 branch of dotnet/winforms repo. the issue has been fixed that have same test result as above
Verified the issue with .NET SDK 9.0.4 + private dlls built from release/9.0 branch of dotnet/winforms repo. the issue has been fixed that have same test result as above
Activity
merriemcgaw commentedon Apr 14, 2025
@Olina-Zhang can your team find when we introduced this?
Syareel-Sukeri commentedon Apr 15, 2025
@merriemcgaw The issue was introduced in .NET 5.0 and is not reproducible in .NET Core 3.1.
Tanya-Solyanik commentedon Apr 15, 2025
@LeafShi1 - could you please investigate what commit introduced this regression?
LeafShi1 commentedon Apr 16, 2025
This issue should be caused by PR #4637, a new event
OnDataSourceDisposedis registered to DataSourceSystem.Windows.Forms.Primitives.dll!Interop.User32.CreateWindowExW(Interop.User32.WS_EX dwExStyle, string lpClassName, string lpWindowName, Interop.User32.WS dwStyle, int X, int Y, int nWidth, int nHeight, System.IntPtr hWndParent, System.IntPtr hMenu, System.IntPtr hInst, object lpParam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams cp) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle() Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get() Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.PointToClient(System.Drawing.Point p) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.MouseOverEditingControl.get() Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.EndEdit(System.Windows.Forms.DataGridViewDataErrorContexts context, System.Windows.Forms.DataGridView.DataGridViewValidateCellInternal validateCell, bool fireCellLeave, bool fireCellEnter, bool fireRowLeave, bool fireRowEnter, bool fireLeave, bool keepFocus, bool resetCurrentCell, bool resetAnchorCell) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.CurrentCell.set(System.Windows.Forms.DataGridViewCell value) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.DataSource.set(object value) Unknown
System.Windows.Forms.dll!System.Windows.Forms.DataGridView.OnDataSourceDisposed(object sender, System.EventArgs e) Unknown
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose(bool disposing) Unknown
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.Dispose(bool disposing) Unknown
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose() Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose(bool disposing) Unknown
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose() Unknown
DataGridViewSample.dll!DataGridViewSample.DataGridViewDialog.Dispose(bool disposing) Line 23 C#
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose(bool disposing) Line 67 C#
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.Dispose(bool disposing) Line 961 C#
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose() Line 61 C#
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose(bool disposing) Line 165 C#
System.ComponentModel.TypeConverter.dll!System.ComponentModel.Container.Dispose() Line 149 C#
DataGridViewSample.dll!DataGridViewSample.DataGridViewDialog.Dispose(bool disposing) Line 21 C#
System.ComponentModel.Primitives.dll!System.ComponentModel.Component.Dispose() Line 61 C#
Tanya-Solyanik commentedon Apr 16, 2025
Great investigation. We probably shouldn't set the current cell on a DGV being disposed
1 remaining item
Add _isReleasingDataSource to prevent unnecessary operations on Curre…
Olina-Zhang commentedon Apr 24, 2025
Verified in the latest .NET 10.0 SDK build: 10.0.100-preview.5.25223.16, it was fixed: no InvalidOperationException pops up now.
GH13304_Fixed.mp4
Tanya-Solyanik commentedon Apr 24, 2025
@Olina-Zhang - we are re-doing this fix a little.
Philip-Wang01 commentedon Apr 30, 2025
Verified in the latest .NET 10.0 SDK build: 10.0.100-preview.5.25229.109, it was fixed: no InvalidOperationException pops up now.
Syareel-Sukeri commentedon May 7, 2025
Verified this issue in the .NET 10 Preview 4 test pass build, it was fixed. The test result is same as above
[release/8.0] InvalidOperationException when closing a dialog with fo…
[release/9.0] InvalidOperationException when closing a dialog with fo…
Zheng-Li01 commentedon May 8, 2025
Verified the issue with .NET SDK 8.0.15 + private dlls built from release/8.0 branch of dotnet/winforms repo. the issue has been fixed that have same test result as above
Philip-Wang01 commentedon May 8, 2025
Verified the issue with .NET SDK 9.0.4 + private dlls built from release/9.0 branch of dotnet/winforms repo. the issue has been fixed that have same test result as above
chrarnoldus commentedon May 8, 2025
Thanks!
MelonWang1 commentedon May 22, 2025
Verified this issue in the .NET 9.0.6 test pass build, it was fixed, same result as above.
Philip-Wang01 commentedon May 23, 2025
Verified this issue in the .NET 8.0.17 test pass build, it was fixed, same result as above.