2017-08-02 88 views
0

首先,我想潜在道歉,因为我是新的Visual Basic(自学),所以我的一些代码可能看起来很奇怪。Visual Basic - 输入执行DataGridView CellContentClick

我加载一个DataGridView的信息由SELECT语句填充,当我点击视图内的某个单元格时,它会加载一个新的表单,这很好。不过,我想知道是否有方法可以将点击更改为“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框架4.5.2

+0

如果你正在使用'vb.net'那么就没有必要还包括'VBA'标签。 – braX

+0

谢谢 - 这已被删除。 – user3482471

+0

网格上有一个Key_Down事件。你可以使用这个。 – SQLAndOtherStuffGuy

回答

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 
+0

相同的代码将在Form_KeyDown事件上工作。 – SQLAndOtherStuffGuy

+0

嗨 - 这真是太棒了。另外一件事 - 当我按下输入时,它也保持上排的功能。您是否意识到能够关闭此功能? – user3482471

+0

没问题。看到这里的潜在解决方案。 https://stackoverflow.com/questions/26096968/preventing-windows-forms-datagridview-moving-to-next-row-on-pressing-enter-key。你可以尝试添加。 'e.SupressKeyPress = True'结尾。我还没有尝试过。 – SQLAndOtherStuffGuy