Visual Basic - 输入以执行DataGridView CellContentClick

时间:2017-08-02 09:54:32

标签: vb.net winforms datagridview basic

首先,我想道歉,因为我是Visual Basic的新手(自学成才)所以我的一些代码可能看起来很奇怪。

我使用SELECT语句填充的信息加载DataGridView,当我点击视图中的某个单元格时,它会加载一个新的表单,这很好。但是,我想知道是否有一种方法可以让我从点击更改为按键" ENTER"。

我附上了我的代码,希望这会有所帮助。

先谢谢你

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Enter Then ' not sure on code here
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If Update_Detail.Visible = True Then Update_Detail.Close()
    Dim istat As Object
    If e.RowIndex > -1 Then istat = DataGridView1.Rows(e.RowIndex).Cells(0).Value
    If e.ColumnIndex < 1 And e.RowIndex > -1 Then Update_Detail.TextBox1.Text = istat
    If e.ColumnIndex < 1 And e.RowIndex > -1 Then Update_Detail.Show()
End Sub

我也在使用Visual Studio 2015和.Net Framework 4.5.2

1 个答案:

答案 0 :(得分:1)

  Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then

            Dim currentRowIndex As Integer = DataGridView1.CurrentCell.RowIndex
            Dim currentColumnIndex As Integer = DataGridView1.CurrentCell.ColumnIndex


            Dim istat As Object

            If currentRowIndex > -1 Then istat = DataGridView1.Rows(currentRowIndex).Cells(0).Value
            If currentColumnIndex < 1 And currentRowIndex > -1 Then Update_Detail.TextBox1.Text = istat
            If currentColumnIndex < 1 And currentRowIndex > -1 Then Update_Detail.Show()

        End If

    End Sub