By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

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

P: 1
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub g_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles g.EditingControlShowing
  2.     Select Case g.CurrentCell.ColumnIndex
  3.         Case 0
  4.             Dim t As TextBox
  5.             t = e.Control
  6.             t.AcceptsReturn = False
  8.             RemoveHandler t.KeyDown, AddressOf txt_KeyDown
  9.             AddHandler t.KeyDown, AddressOf txt_KeyDown
  10.     End Select
  11. End Sub
  13. Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  14.     If e.KeyCode = Keys.Return Then
  15.         Dim t As TextBox = CType(sender, TextBox)
  16.         dtb = rst("select * from customer where customerno=" & Val(t.Text))
  17.         If Not dtb.Rows.Count = 0 Then
  18.             g.Rows(g.CurrentRow.Index).Cells(1).Value = dtb.Rows(0)("customername").ToString()
  19.         End If
  20.     End If
  21. End Sub
Oct 1 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.