user2674855
user2674855

Reputation: 225

capture keyevent in CellEndEdit of data grid view in vb.net

my datagridview name is DGVall i given code in DGVall_CellEndEdit event

If e.ColumnIndex = 2 Then

        If DGVall.CurrentRow.Cells(2).Value = "" Then
            MessageBox.Show("Please Enter Driver ID")
            Exit Sub
        End If

befor this i want to check wethar i pressed Enter only ..if i pressed enter only i want to execute this.but i am not able to check here wethar i am pressed which control.i am working on vb.net windows application

Upvotes: 0

Views: 2113

Answers (1)

Doan Cuong
Doan Cuong

Reputation: 2614

You can use an approach like this

Private isEnterPress as boolean = false

private sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
    If dgv.CurrentCell.ColumnIndex = 2 'if column index = 2 then add handler to control
        AddHandler Ctype(e.Control, Textbox).KeyPress, AddresOf TextBox_KeyPress
    End If
End Sub

private sub TextBox_keyPress(byval sender as object, byval e as KeyPressEventArgs)
    If 'check if key press is enter key
        isEnterPress = true
    end if
End Sub

Then in CellEndEdit event, you can check isEnterPress and perform action base on its value. But remember set isEnterPress to false when you finish. Hoep this help

NOte: I'm sorry that I forgot how to check if key enter is press and I'm not at my working computer now.

Upvotes: 1

Related Questions