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

时间:2015-06-14 01:42:13

标签: vb.net ms-access datagridview

我在VB.Net中使用DataGridView编写应用程序

在DataGridView上,Cell(列(0),行(0))用于在项目代码中搜索数据库。我想要的是当在数据库中找不到商品代码时,光标应该回到Cell(第(0)行,第(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

真的需要帮助..请帮助我......

1 个答案:

答案 0 :(得分: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 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