In this article I will explain with an example, how to transfer (copy) selected (checked) rows from one DataGridView to another DataGridView in Windows Forms (WinForms) Application using C# and VB.Net.
When the Transfer button is clicked, a loop will be executed and all the rows of DataGridView for which the CheckBox is checked (selected) will be transferred (copied) to another DataGridView in Windows Forms (WinForms) Application using C# and VB.Net.
Form Controls
The below Form consists of two DataGridView control and a Button.
Namespaces
You will need to import the following namespace.
C#
VB.Net
Populating DataGridView
Inside the Form Load event, the DataGridView is populated with a dynamic DataTable with some dummy records.
Once the DataGridView is populated, dynamically a CheckBox column is added to the DataGridView in first column.
C#
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;
//Add a CheckBox Column to the DataGridView at the first position.
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim dt As 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
'Add a CheckBox Column to the DataGridView at the first position.
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
dataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
Transfer (Copy) Selected (Checked) rows from DataGridView to another DataGridView
When the Transfer Button is clicked, a loop is executed over the DataGridView rows. Inside the loop, a check is performed whether the CheckBox is checked (selected) or not.
If the CheckBox is checked (selected), the values from each column (cell) of the DataGridView is fetched and is added to a DataTable.
Finally the DataTable is used to populate the other DataGridView.
C#
private void btnTransfer_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("CustomerId");
dt.Columns.Add("Name");
dt.Columns.Add("Country");
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);
}
}
dataGridView2.DataSource = dt;
}
VB.Net
Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
Dim dt As New DataTable()
dt.Columns.Add("CustomerId")
dt.Columns.Add("Name")
dt.Columns.Add("Country")
For Each row As DataGridViewRow In dataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
If isSelected Then
dt.Rows.Add(row.Cells(1).Value, row.Cells(2).Value, row.Cells(3).Value)
End If
Next
dataGridView2.DataSource = dt
End Sub
Screenshot
Downloads