Search code examples
vb.netwinformsdatagridviewbasic

Visual Basic - Enter to Perform DataGridView CellContentClick


First i would like to potentially apologise as i am new to Visual Basic (self taught) so some of my code may look odd.

I load a DataGridView with information populated by a SELECT Statement, when i click on a certain cell within the view it loads a new form which is fine. However i would like to know if there is a way in which i can change from a click to a key press of "ENTER".

I have attached my code, hopefully this helps.

thank you in Advance

    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

Also I am using Visual Studio 2015 with .Net Framework 4.5.2


Solution

  •   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