Home > Blockchain >  Show data on enter or tab in a datagridvied vb.net
Show data on enter or tab in a datagridvied vb.net

Time:10-07

I have problems when I do the following process.

When I type in a cell of the following datagridview and when pressing tab or enter, the record I am looking for is not displayed.

I have the following code that repeats what I am looking for and 1 automatic row is added when pressing enter.

Sub mostrarproductosbonus()

    dt = negLog.buscar_pro_parametro(VGlobales.Base, Me.dgvdetalle.CurrentRow.Cells(0).Value)

    For Each data As DataRow In dt.Rows
        Dim aa As Integer = Me.dgvdetalle.Rows.Add()
        ' Me.dgvproductoscanjes.Rows(aa).Cells(0).Value = data("seleccionar").ToString().Trim
        Me.dgvdetalle.Rows(aa).Cells(0).Value = data("IDPRODUCTO").ToString()
        Me.dgvdetalle.Rows(aa).Cells(1).Value = data("DESCRIPCION").ToString()

    Next

End Sub

enter event code of datagridview

Private Sub dgvdetalle_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgvdetalle.KeyPress
    If Keys.Enter = 13 Then
        mostrarproductosbonus()
    End If
End Sub

This code, although true, works at the first enter but then when I want to look for another product with its code, it is not added, the one that is already added is repeated.

They will have some solution or help for this problem or in which I am failed.

enter image description here

CodePudding user response:

The KeyPress event doesn't detect the Enter key. Use the KeyUp or KeyDown event.

Keys.Enter will always equal 13. That is its value in the Enumeration. Your Sub would run on every KeyPress. Use the value from the event args in the KeyDown event.

Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp
    If e.KeyCode = 13 Then
        MessageBox.Show("You pressed Enter")
    End If
End Sub

Just substitute you grid event DataGridView1 and the Sub you want to call for the MessageBox.

  • Related