2017-08-02 91 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