Capture ENTER key in vb.net datagrid

  • Thread starter Thread starter Melson
  • Start date Start date
M

Melson

hi

can anyone help me how can i capture ENTER keystroke when the cell in
datagrid is in editing mode.

I'm now creating a data entry form with primary key in header and details in
datagrid. So when user key in the details in datagrid, I would like user to
choose product code by pressing ENTER key in the datagrid and a Product Code
dialog will appear for him to choose.

Pls help. Thanks very much.

Regards
Steve
 
Steve,

To capture keypress event in a datagrid you should use the ProcessCmdKey
function.

for example:
Public Class MyDataGrid
Inherits DataGrid

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As
Boolean
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
SendKeys.Send("{Tab}")
Return True
End If

OR
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case (Keys.Control Or Keys.M)
Case (Keys.Alt Or Keys.Z)
Case Keys.Delete
End Select
End If

Return MyBase.ProcessCmdKey(msg, keyData)
End Function 'ProcessCmdKey

End Class 'MyDataGrid


Greetinx,
Foef
 
Hi Foef

Thank you very much for your solution. Can you tell me how can i use your
code into my existing project. The code is as below. I'm sorry because I'm
new to VB.NET. I'm using Visual Studio ver 7.1. Is there any sample code I
can download.

Pls help. Thanks.

Regards
Steve

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' create a connection string
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Northwind.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = connString

' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from
Customers", myConnection)

' create a new dataset
Dim ds As DataSet = New DataSet()
' fill dataset
da.Fill(ds, "Customers")

' Attach DataSet to DataGrid
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
End Class
 
Hi Foef

Thanks very much. I've found the way to create Windows Control Library and
add to my windows application. Thanks again


Regards
Steve
 
Back
Top