Form with DataGridView being edited cannot be closed

Asked 2 years ago, Updated 2 years ago, 147 views

The following sources in Microsoft Visual Basic 2015 are not working, is there anything wrong (missing)?

Private SubDataGridView_Leave (sender As Object, eAs EventArgs) Handles DataGridView.Leave
    Dimdgv As DataGridView= DirectCast (sender, DataGridView)

    If dgv.IsCurrentCellInEditMode Then
        dgv.CurrentCell= Nothing
    End If

    Me. Close()
End Sub

Supplemental:
If it is not in the edit state (F2 press), the form closes when the focus leaves DataGridView.

vb.net

2022-09-30 21:37

1 Answers

I haven't found a perfect solution yet, but I think I can do the following.

Private Sub Me_FormClosing (sender As Object, eAs FormClosingEventArgs) Hands MyBase.FormClosing
    If ForceToCloseForm Then
        e.Cancel=False
    End If
End Sub

Private Sub DataGridView_Leave (sender As Object, eAs EventArgs) Handles DataGridView.Leave
    Dimdgv As DataGridView= DirectCast (sender, DataGridView)

    If dgv.IsCurrentCellInEditMode Then
        dgv.CurrentCell= Nothing
        ForceToCloseForm=True
    End If

    Me. Close()
End Sub


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.