Skip to content

InvalidOperationException when dialog containing focused DataGridView is closed #13304

Description

@chrarnoldus

.NET version

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.

DataGridViewCrash.zip

DataGridViewCrash.mp4

Activity

  1. added
    untriaged
    on Apr 14, 2025
  2. added
    area-VSDesigner
    and removed
    area-VSDesigner
    on Apr 14, 2025
  3. merriemcgaw commented on Apr 14, 2025

    @merriemcgaw
    Contributor

    @Olina-Zhang can your team find when we introduced this?

  4. Syareel-Sukeri commented on Apr 15, 2025

    @Syareel-Sukeri
    Contributor

    @merriemcgaw The issue was introduced in .NET 5.0 and is not reproducible in .NET Core 3.1.

  5. added
    💥 regression-release
    on Apr 15, 2025
  6. added theissue type on Apr 15, 2025
  7. removed
    untriaged
    on Apr 15, 2025
  8. added this to the .NET 10.0 milestone on Apr 15, 2025
  9. Tanya-Solyanik commented on Apr 15, 2025

    @Tanya-Solyanik
    Contributor

    @LeafShi1 - could you please investigate what commit introduced this regression?

  10. LeafShi1 commented on Apr 16, 2025

    @LeafShi1
    Member

    This issue should be caused by PR #4637, a new event OnDataSourceDisposed is registered to DataSource

    • The call stack in .net5.0:

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

    • The call stack in .net3.1:

    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#

  11. Tanya-Solyanik commented on Apr 16, 2025

    @Tanya-Solyanik
    Contributor

    Great investigation. We probably shouldn't set the current cell on a DGV being disposed

  12. 1 remaining item

  13. added a commit that references this issue on Apr 23, 2025
    b33f1ba
  14. Olina-Zhang commented on Apr 24, 2025

    @Olina-Zhang
    Member

    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
  15. Tanya-Solyanik commented on Apr 24, 2025

    @Tanya-Solyanik
    Contributor

    @Olina-Zhang - we are re-doing this fix a little.

  16. Philip-Wang01 commented on Apr 30, 2025

    @Philip-Wang01
    Contributor

    Verified in the latest .NET 10.0 SDK build: 10.0.100-preview.5.25229.109, it was fixed: no InvalidOperationException pops up now.

  17. Syareel-Sukeri commented on May 7, 2025

    @Syareel-Sukeri
    Contributor

    Verified this issue in the .NET 10 Preview 4 test pass build, it was fixed. The test result is same as above

  18. added 2 commits that reference this issue on May 8, 2025
    a29c04a
    1390aa6
  19. Zheng-Li01 commented on May 8, 2025

    @Zheng-Li01
    Member

    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

  20. Philip-Wang01 commented on May 8, 2025

    @Philip-Wang01
    Contributor

    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

  21. chrarnoldus commented on May 8, 2025

    @chrarnoldus
    Author

    Thanks!

  22. MelonWang1 commented on May 22, 2025

    @MelonWang1
    Contributor

    Verified this issue in the .NET 9.0.6 test pass build, it was fixed, same result as above.

  23. Philip-Wang01 commented on May 23, 2025

    @Philip-Wang01
    Contributor

    Verified this issue in the .NET 8.0.17 test pass build, it was fixed, same result as above.

  24. locked and limited conversation to collaborators on Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions