In this article I will explain with an example, how to get Cell Value of selected DataGridView Row in Windows Application (WinForms) using C# and VB.Net.
When the DataGridView Row is clicked, the Row Index of the selected DataGridView Row is determined and the values of the Cells are extracted and displayed in TextBoxes in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
The Form consists of a DataGridView and three TextBoxes. The DataGridView has been assigned CellMouseClick event handler.
Get Cell Value of Selected DataGridView Row in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Populating the DataGridView
Inside the Form Load event handler, the DataGridView is populated with data by making use of a dynamic DataTable with some records.
C#
private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.AllowUserToAddRows = false;
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), _
                                           New DataColumn("Name", GetType(String)), _
                                           New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
    Me.dataGridView1.AllowUserToAddRows = False
End Sub
 
 
Get Cell Value of Selected DataGridView Row
When the DataGridView Row or Cell is clicked, the Row Index of the clicked DataGridView Row is determined and the values of the Cells are extracted and displayed in TextBoxes in Windows Forms (WinForms) Application using C# and VB.Net.
C#
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        txtID.Text = row.Cells[0].Value.ToString();
        txtName.Text = row.Cells[1].Value.ToString();
        txtCountry.Text = row.Cells[2].Value.ToString();
    }
}
 
VB.Net
Private Sub dataGridView1_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dataGridView1.CellMouseClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
        txtID.Text = row.Cells(0).Value.ToString
        txtName.Text = row.Cells(1).Value.ToString
        txtCountry.Text = row.Cells(2).Value.ToString
    End If
End Sub
 
 
Screenshot
Get Cell Value of Selected DataGridView Row in Windows Application using C# and VB.Net
 
 
Downloads