当我编辑单元格并按 ENTER 我想更新数据库

When I edit a cell and press ENTER I want to update the database

让我们看看是否有人可以帮助我。

我希望当我在 DataGridView 中编辑单元格并按下 ENTER 键时更新数据库。

我有一个按钮可以对我执行此操作,但只有在编辑它之后(不按 ENTER),我按下按钮,它才会更新数据库。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' we will create save functions in our module
        Dim row, ID As Integer
        Dim Nombre, Apellidos, DNI, Telefono, Email As String
        ' Declare the variable to get value event click on datagridview
        row = DataGridView1.CurrentRow.Index
        ID = DataGridView1(0, row).Value
        Nombre = DataGridView1(1, row).Value
        Apellidos = DataGridView1(2, row).Value
        DNI = DataGridView1(3, row).Value
        Telefono = DataGridView1(4, row).Value
        Email = DataGridView1(5, row).Value
        ' query to Update data into biodata tables
        Dim UpdateData As String = "UPDATE tbl_biodata SET Nombre='" & Nombre & "',Apellidos='" & Apellidos & "',DNI='" & DNI & "',Telefono='" & Telefono & "',Email='" & Email & "' WHERE ID=" & ID & ""
        ' call function to update data
        RunSQL(UpdateData)
        ' fill new data into datagridview1
        showData()
    End Sub

您可以使用 CellEndEdit 事件。只需指定您的列号。可能您必须检查是否填充了所有需要的单元格(在查询中使用)。

Private Sub DataGridView1_EndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    If e.ColumnIndex = 5 Then 'replace 5 with your column number
        Button2.PerformClick()
    End If
End Sub

此致

在窗体设计器中,select 窗体并打开属性。将窗体的 AcceptButton 属性 设置为 Button2。当您按下 Enter 键时,Button2 事件处理程序中的代码将 运行。