How to program DataGrid Keypress event? like in VB6

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear all,

I used to code keypress event for VB6 as below. But now I can't upgrade this code to VBdot net 2003. Can any one help me please, to sort out the code to match VB dot net 2003?

Thanks for your help..

VB.NET:
 Private Sub DataGrid1_KeyPress(KeyAscii As Integer)
  
  If KeyAscii = vbKeyReturn Then
        KeyAscii = 0
        
        DataGrid1.Refresh
        
       
        cons.Connect
   
        On Error Resume Next
      strsql = "Select * from ProductMaster where Prodcode='" & DataGrid1.Columns(0).Text & "'"
   
      Set rst = New ADODB.Recordset
      rst.Open strsql, Conn, adOpenKeyset, adLockOptimistic
      If rst.RecordCount <= 0 Or rst.BOF Or rst.EOF Then
        MsgBox DataGrid1.Columns(0).Text + "  Not found in register"
        On Error Resume Next
        
        Exit Sub
       
       End If
        
    If DataGrid1.Col = 0 Then
   
      
      DataGrid1.Columns(1).Text = rst(1)
      DataGrid1.Columns(2).Text = rst(3)
        SendKeys "{TAB}" + "{TAB}" + "{TAB}", True
                  
    ElseIf DataGrid1.Col = 3 Then
        
       DataGrid1.Columns(4).Text = DataGrid1.Columns(2).Text * DataGrid1.Columns(3).Text
        
        On Error Resume Next
       
        Adodc1.Recordset.Requery
        Call sumtotal
        SendKeys "{PGDN}", True
    End If
 End If
        cons.Disconnect
End Sub
 
The DataGrid control has a KeyPress event and many other events. In code view select the control in upper left combobox, then select the event in upper right combobox, the event handler code is now inserted in your code and you can write the code you want when event happens. For example to see if it was Enter key use this code:
VB.NET:
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
 
End If
 
How could we translate DG.Colnumber as in VB6

Thank you very much JohnH,

Here I started to code in vb.net as below. But I could not complete it with regard to column number as stated down.can any one help me to translate this code from VB6 please ?

VB.NET:
'VB Dot net incompleted code
Private Sub dgInvoice_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgInvoice.KeyUp
If e.KeyCode = Keys.Enter Then
If dgInvoice.CurrentCell.ColumnNumber = 1 Then
dgInvoice.
End If
End If

Here are the code I wanted to translate from vb6 to VBdot net and put them inside above code as required:-

VB.NET:
strsql = "Select * from ProductMaster where Prodcode='" & DataGrid1.Columns(0).Text & "'"
VB.NET:
If DataGrid1.Col = 0 Then
   
      
      DataGrid1.Columns(1).Text = rst(1)
      DataGrid1.Columns(2).Text = rst(3)
        SendKeys "{TAB}" + "{TAB}" + "{TAB}", True
                  
    ElseIf DataGrid1.Col = 3 Then
        
       DataGrid1.Columns(4).Text = DataGrid1.Columns(2).Text * DataGrid1.Columns(3).Text
End if
Your help is most appreciated
 
It's been a long time since I used .Net 1.1 and the DataGrid control, but found there is problems with the Keypresses events, usually it is the textbox in the cell that is active and raises the event. There could be different ways of handling this, have a look though Winforms FAQ for DataGrid control for example. Here is another, set the forms KeyPreview property True and handle its event:
VB.NET:
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    If Me.ActiveControl.Parent Is DG AndAlso e.KeyCode = Keys.Enter Then
 
    End If
End Sub
It looks as you want the column name, you get this from what you set as datasource for the datagrid. For example if it is a DataTable:
VB.NET:
Dim dt As DataTable = DirectCast(DG.DataSource, DataTable)
Dim CurrentColumnName As String = dt.Columns(DG.CurrentCell.ColumnNumber).ColumnName
 
Back
Top