在数据网格中的输入或选项卡上显示数据 vb.net

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

我在做下面的过程时遇到了问题

当我在下面的datagridview的单元格中输入时,当按tab或回车时,没有显示我要查找的记录。

我有以下代码重复了我正在寻找的内容,并且在按 enter 时添加了 1 个自动行。

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

输入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

此代码虽然正确,但在第一次输入时有效,但是当我想用它的代码查找另一个产品时,它没有被添加,已经添加的那个被重复了。

他们会针对这个问题或我失败的地方提供一些解决方案或帮助。

KeyPress 事件未检测到 Enter 键。使用 KeyUpKeyDown 事件。

Keys.Enter 将始终等于 13。那就是它在Enumeration中的值。您的 Sub 会在每个 KeyPress 上 运行。使用 KeyDown 事件中事件参数的值。

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

只需将您的网格事件 DataGridView1 和您要调用的 Sub 替换为 MessageBox