VB.Net-MySQL : select row in DataGridView on KeyDown event to show data in TextBox

1. Create your database and table (MySQL)


2. Design your  Window Form


3. Code behind form

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class bookissue
    Private Sub bookissue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = GetData("select * from book")
        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        With DataGridView1
            .RowHeadersVisible = False
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
            .CellBorderStyle = DataGridViewCellBorderStyle.Single
            .GridColor = Color.Black

            .Columns(0).HeaderCell.Value = "Book Code"
            .Columns(1).HeaderCell.Value = "Book Name"
            .Columns(2).HeaderCell.Value = "Author"
            .Columns(3).HeaderCell.Value = "Publisher"
            .Columns(4).HeaderCell.Value = "Quantity"
        End With
    End Sub
    Private Shared Function GetData(ByVal selectCommand As String) As DataTable
        Dim cnn As New MySqlConnection
        Dim data As New DataTable()
        cnn = New MySqlConnection
        cnn.ConnectionString = "Server=;Database=library;Uid='root';Pwd='';"
        Dim cmd As MySqlCommand = New MySqlCommand()
        cmd.Connection = cnn
        cmd.CommandText = selectCommand
        Dim ada As MySqlDataAdapter = New MySqlDataAdapter()
        ada.SelectCommand = cmd
        Return data
    End Function
    Protected Sub dataGridView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.DataGridView1.CurrentRow.Selected = True
            Dim selectedRowCount As Integer = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
            If selectedRowCount = 1 Then
                TextBox1.Text = DataGridView1.Item("bname", DataGridView1.CurrentRow.Index).Value
                TextBox2.Text = DataGridView1.Item("bauthor", DataGridView1.CurrentRow.Index).Value
                TextBox3.Text = DataGridView1.Item("bpublisher", DataGridView1.CurrentRow.Index).Value
                TextBox4.Text = DataGridView1.Item("bquantity", DataGridView1.CurrentRow.Index).Value
            End If

            e.Handled = True

        End If
    End Sub
End Class

4. Load and Run the  the formcodetpoint5.  Above  require  .Net/MySQL connector . Please  refer  <click Here>