Hi ishuhasan21,
Please refer below sample.
Note: For this sample i used article. For more details refer below article link.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
using System.Data;
using System.Data.SqlClient;
Code
Form1
private void Form1_Load(object sender, EventArgs e)
{
string constring = @"Data Source=.\SQL2019;DataBase=NORTHWIND;UID=sa;PWD=pass@123";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT ProductName,UnitsInStock,ReorderLevel FROM Products", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
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)
{
DataTable dt = new DataTable();
dt.Columns.Add("ProductName");
dt.Columns.Add("UnitsInStock");
dt.Columns.Add("ReorderLevel");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
if (isSelected)
{
dt.Rows.Add(row.Cells[1].Value, row.Cells[2].Value, row.Cells[3].Value);
}
}
Form2 form2 = new Form2(dt);
form2.Show();
this.Hide();
}
Form2
public Form2(DataTable dt)
{
InitializeComponent();
dataGridView1.DataSource = dt;
}
Screenshot