Hi pratikshir,
Check this example. Now please take its reference and correct your code.
Code
Form1
C#
public partial class Form1 : Form
{
public static string id { get; set; }
public Form1()
{
InitializeComponent();
this.CenterToScreen();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGridView();
}
private void BindDataGridView()
{
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;
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
}
private void btnTransfer_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
id = row.Cells[1].Value.ToString();
Form2 form2 = new Form2();
form2.Show();
break;
}
}
}
}
VB.Net
Imports System.Data
Public Class Form1
Public Shared Property id As String
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {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
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Private Sub btnTransfer_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each row As DataGridViewRow In dataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
If isSelected Then
id = row.Cells(1).Value.ToString()
Dim form2 As Form2 = New Form2()
form2.Show()
Exit For
End If
Next
End Sub
End Class
Form2
C#
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.CenterToScreen();
}
private void Form2_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");
DataRow[] dr = dt.Select("Id='" + Form1.id + "'");
if (dr.Length > 0)
{
lblId.Text = dr[0]["Id"].ToString();
txtName.Text = dr[0]["Name"].ToString();
txtCountry.Text = dr[0]["Country"].ToString();
}
}
}
VB.Net
Public Class Form2
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {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")
Dim dr As DataRow() = dt.[Select]("Id='" & Form1.id & "'")
If dr.Length > 0 Then
lblId.Text = dr(0)("Id").ToString()
txtName.Text = dr(0)("Name").ToString()
txtCountry.Text = dr(0)("Country").ToString()
End If
End Sub
End Class
Screenshot