In this article I will explain with an example, how to pass (send) values from one Form to another in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
Form1
The Form consists of following control:
DataGridView – For displaying data.
The DataGridView has been assigned with following event handler.
Events
CellMouseClick � Triggered when any cell inside the DataGridView is clicked.
Form2
The Form consists of following control:
Label � For labelling and displaying data sent from Form1.
Namespaces
You will need to import the following namespace.
C#
VB.Net
Populating the DataGridView using C# and VB.Net
Inside the Form_Load event handler, the DataGridView is populated with dynamic DataTable.
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");
dataGridView1.DataSource = dt;
dataGridView1.AllowUserToAddRows = false;
}
VB.Net
Private Sub Form1_Load(sender As Object, e As 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")
dataGridView1.DataSource = dt
dataGridView1.AllowUserToAddRows = False
End Sub
Passing (Sending) the Selected Row of DataGridView to another Form
Inside the Form1, a static property SelectedRow is declared which will be used to pass the selected row values to another Form.
When the DataGridView Row or Cell is clicked, a check is performed if cell of the Data Row is clicked. If clicked, then the clicked row is determined and is assigned to the SelectedRow property.
Finally, the Form2 is opened using show method.
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 Object, e As 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 cell values of Selected Row are fetched from the SelectedRow property of the Form1 and then displayed in Label controls.
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 Object, e As 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
Downloads