Catch Enter Key with KeyDown Event in Text Column of DataGridVIew Conrol

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yekokoaung
    New Member
    • Oct 2013
    • 1

    Catch Enter Key with KeyDown Event in Text Column of DataGridVIew Conrol

    I want to catch Enter Key with KeyDown Event in TextColumn of DataGridView Control. When I press Enter in the txtItemCode column, I will check the ItemCode in the Database and other stuffs. I can get other keys but I can't get Enter. When I hit the Enter key, The focus is go to next row same column. How could I catch the Enter Key. I can catch with PreviewKeyDown Event but The Event occur from other columns also. I just want to catch from single column. I already googled but found nothing. Below is my code. Help me please.

    Code:
    Private Sub g_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles g.EditingControlShowing
        Select Case g.CurrentCell.ColumnIndex
            Case 0
                Dim t As TextBox
                t = e.Control
                t.AcceptsReturn = False
    
                RemoveHandler t.KeyDown, AddressOf txt_KeyDown
                AddHandler t.KeyDown, AddressOf txt_KeyDown
        End Select
    End Sub
    
    Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Return Then
            Dim t As TextBox = CType(sender, TextBox)
            dtb = rst("select * from customer where customerno=" & Val(t.Text))
            If Not dtb.Rows.Count = 0 Then
                g.Rows(g.CurrentRow.Index).Cells(1).Value = dtb.Rows(0)("customername").ToString()
            End If
        End If
    End Sub
    Last edited by Rabbit; Oct 1 '13, 03:02 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
Working...