In this article I will explain with an example, how to pass (send) values from one Form to another 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 passed (sent) to the other Form in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
Form1
The Form1 consists of a DataGridView which has been assigned CellMouseClick event handler.
Pass (Send) values from one Form to another in Windows Application using C# and VB.Net
 
Form2
The Form1 consists of three Labels.
Pass (Send) values from one Form to another 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
 
 
Passing (Sending) the Selected Row of DataGridView to another Form
When the DataGridView Row or Cell is clicked, the Row Index of the clicked DataGridView Row is determined and the selected DataGridView Row is assigned to the SelectedRow property.
C#
public static DataGridViewRow SelectedRow { get; set; }
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        //Set the Selected Row in Property.
        SelectedRow = dataGridView1.Rows[e.RowIndex];
 
        //Open the other Form.
        Form2 form2 = new Form2();
        form2.Show();
    }
}
 
VB.Net
Public Shared Property SelectedRow As DataGridViewRow
Private Sub dataGridView1_CellMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dataGridView1.CellMouseClick
    If e.RowIndex >= 0 Then
        'Set the Selected Row in Property.
        SelectedRow = dataGridView1.Rows(e.RowIndex)
 
        'Open the other Form.
        Dim form2 As Form2 = New Form2
        form2.ShowDialog()
    End If
End Sub
 
 
Displaying the DataGridView Selected Row values on another Form
Inside the Form Load event handler, the selected DataGridView Row Cell values are extracted and displayed in Labels in Windows Forms (WinForms) Application using C# and VB.Net.
C#
private void Form2_Load(object sender, EventArgs e)
{
    lblID.Text = Form1.SelectedRow.Cells[0].Value.ToString();
    lblName.Text = Form1.SelectedRow.Cells[1].Value.ToString();
    lblCountry.Text = Form1.SelectedRow.Cells[2].Value.ToString();
}
 
VB.Net
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    lblID.Text = Form1.SelectedRow.Cells(0).Value.ToString()
    lblName.Text = Form1.SelectedRow.Cells(1).Value.ToString()
    lblCountry.Text = Form1.SelectedRow.Cells(2).Value.ToString()
End Sub
 
 
Screenshot
Pass (Send) values from one Form to another in Windows Application using C# and VB.Net
 
 
Downloads