CYB
CYB

Reputation: 1106

DataGridView EndEdit VB

Now, I use EndEdit in Leave EventArgs, but when I use EndEdit, I cannot close my form. Does anyone know the reason ?

Below is my leave function:

 Private Sub dgv_Leave(ByVal sender As System.Object, 
                       ByVal e As System.EventArgs) Handles dgv.Leave
            Me.dgv.EndEdit()
    End Sub

Upvotes: 0

Views: 2237

Answers (2)

CYB
CYB

Reputation: 1106

I found the solution to solve the EndEdit Problem. Then, I could end editing correctly and I could close my form successfully.

http://dobon.net/vb/bbs/log3-35/21499.html

Public Class Form4_4
    Inherits Form

    Dim WithEvents m_dgv As New DataGridView()
    Dim WithEvents m_editingcontrol As Control

    Private Sub Form4_4_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        m_dgv.Dock = DockStyle.Fill
        m_dgv.RowCount = 10
        m_dgv.ColumnCount = 3
        Me.Controls.Add(m_dgv)
    End Sub

    Private Sub m_dgv1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles m_dgv.CellEndEdit
        Me.m_editingcontrol = Nothing ' ← Cancel EventHandler Here
        Console.WriteLine("CellEndEdit")
    End Sub

    Private Sub m_dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles m_dgv.EditingControlShowing
        Console.WriteLine("EditingControlShowing")
        Me.m_editingcontrol = e.Control ' ← Register EventHandler Here
    End Sub

    Private Sub m_editingcontrol_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles m_editingcontrol.PreviewKeyDown
        If e.KeyCode = Keys.Return Then
            Me.m_dgv.EndEdit()
        End If
    End Sub
End Class

Upvotes: 0

Santosh Panda
Santosh Panda

Reputation: 7341

This could be due to some validation error might be present in your DataGridView. You have to check that dataGridView.EndEdit() succeeded and you may also need to check your Cell and RowValidating events if present.

Upvotes: 1

Related Questions