移动到DataGridView VB.Net的早期单元格

移动到DataGridView VB.Net的早期单元格,vb.net,ms-access,datagridview,Vb.net,Ms Access,Datagridview,我正在用VB.Net中的DataGridView编写一个应用程序 在DataGridView的Cellcolumn0上,行0用于在数据库中搜索项代码。我想要的是,当在数据库中找不到项目代码时,光标应该回到Cellcolumn0的第0行,这是我用来搜索项目代码的单元格 以下是我的代码: Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEvent

我正在用VB.Net中的DataGridView编写一个应用程序

在DataGridView的Cellcolumn0上,行0用于在数据库中搜索项代码。我想要的是,当在数据库中找不到项目代码时,光标应该回到Cellcolumn0的第0行,这是我用来搜索项目代码的单元格

以下是我的代码:

 Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellEndEdit
    Dim flag_cell_edited As Boolean
    Dim CurrentRow As Integer
    Dim CurrentColumn As Integer
    If e.ColumnIndex = 0 Then
        flag_cell_edited = True
        CurrentRow = e.RowIndex
        CurrentColumn = e.ColumnIndex
        Call Koneksi()
        CMD = New OleDbCommand("Select Nama_Matakuliah, SKS from tbmatakuliah where Kode_MK = '" & DGV1.Rows(e.RowIndex).Cells(0).Value & "' and Program_Studi = '" & txt_ps.Text & "'", CONN)
        DR = CMD.ExecuteReader
        DR.Read()
        If DR.HasRows Then
            DGV1.Rows(e.RowIndex).Cells(1).Value = DR.Item("Nama_Matakuliah")
            DGV1.Rows(e.RowIndex).Cells(2).Value = DR.Item("SKS")
            DGV1.Rows(e.RowIndex).Cells(1).ReadOnly = True
            DGV1.Rows(e.RowIndex).Cells(2).ReadOnly = True
            DGV1.Rows(e.RowIndex).Cells(3).Selected = True
        Else
            MsgBox("Kode Mata Kuliah " & DGV1.Rows(e.RowIndex).Cells(0).Value & " Tidak ditemukan", MsgBoxStyle.OkOnly, "Pengisian Kartu Hasil Studi")
            DGV1.CurrentCell = DGV1(CurrentColumn, CurrentRow)
            flag_cell_edited = False
        End If
    End If
End Sub
我真的需要帮助。。请帮帮我

也许是这样

Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellEndEdit
'
Dim flag_cell_edited As Boolean
Dim LastRow As Integer
Dim LastColumn As Integer
Dim CurrentRow As Integer
Dim CurrentColumn As Integer
'
  LastColumn = e.ColumnIndex
  LastRow = e.RowIndex
  If e.ColumnIndex = 0 Then
    flag_cell_edited = True
    CurrentRow = e.RowIndex
    CurrentColumn = e.ColumnIndex
    Call Koneksi()
    CMD = New OleDbCommand("Select Nama_Matakuliah, SKS from tbmatakuliah where Kode_MK = '" & DGV1.Rows(e.RowIndex).Cells(0).Value & "' and Program_Studi = '" & txt_ps.Text & "'", CONN)
    DR = CMD.ExecuteReader
    DR.Read()
    If DR.HasRows Then
        DGV1.Rows(e.RowIndex).Cells(1).Value = DR.Item("Nama_Matakuliah")
        DGV1.Rows(e.RowIndex).Cells(2).Value = DR.Item("SKS")
        DGV1.Rows(e.RowIndex).Cells(1).ReadOnly = True
        DGV1.Rows(e.RowIndex).Cells(2).ReadOnly = True
        DGV1.Rows(e.RowIndex).Cells(3).Selected = True
    Else
        MsgBox("Kode Mata Kuliah " & DGV1.Rows(e.RowIndex).Cells(0).Value & " Tidak ditemukan", MsgBoxStyle.OkOnly, "Pengisian Kartu Hasil Studi")
        DGV1.CurrentCell = DGV1(CurrentColumn, CurrentRow)
        flag_cell_edited = False
        DGV1.CurrentCell = DGV1(LastColumn, LastRow)
    End If
  End If
End Sub

谢谢你的回答。我会给你一个机会,我会让你知道我已经尝试了你的答案扎夫汗,但它仍然不工作。。。我想换一种方式对不起,我在上面的代码中输入了一个错误,行DGV1.CurrentCell=LastColumn,LastRow应该写为DGV1.CurrentCell=DGV1LastColumn,LastRow