How to Copy a Selected Row From one DataGridView to another DataGridView in C#
By FoxLearn 1/22/2025 7:21:08 AM 12.87K
In this article, we explore how to implement functionality for copying rows of data between two DataGridViews in a Windows Forms application. The provided code demonstrates how to handle copying data from one DataGridView to another based on the selection of checkboxes, making use of data binding to interact with a database.
How to Copy a Selected Row From one DataGridView to another DataGridView in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CopyRowDataGridView" and then click OK
Drag and drop the DataGridView, Button controls from the Visual Toolbox onto your form designer, then design your form as shown below.
Create an EF Model, then select Category table from Northwind database
On the Form1_Load
event, we use Entity Framework
to load data from the Categories
table into the DataGridView.
private void Form1_Load(object sender, EventArgs e) { // Load data from SQL database to DataGridView using (NorthwindEntities db = new NorthwindEntities()) { categoryBindingSource.DataSource = db.Categories.ToList(); } }
We create an instance of NorthwindEntities
, then we use the categoryBindingSource
to bind the Categories
data to the DataGridView.
The functionality for copying rows from one DataGridView
to another is handled by two buttons (btnLeft
and btnRight
).
// Copying from Right to Left DataGridView private void btnLeft_Click(object sender, EventArgs e) { // Copy rows from DataGridView (Right) to DataGridView (Left) for (int i = dataGridViewRight.RowCount - 1; i >= 0; i--) { DataGridViewRow row = dataGridViewRight.Rows[i]; if (Convert.ToBoolean(row.Cells["colRightSelected"].Value)) { categoryBindingSource.Add((Category)row.DataBoundItem); categoryBindingSource1.RemoveAt(row.Index); } } } // Copying from Left to Right DataGridView private void btnRight_Click(object sender, EventArgs e) { // Copy rows from DataGridView (Left) to DataGridView (Right) for (int i = dataGridViewLeft.RowCount - 1; i >= 0; i--) { DataGridViewRow row = dataGridViewLeft.Rows[i]; if (Convert.ToBoolean(row.Cells["colLeftSelected"].Value)) { categoryBindingSource1.Add((Category)row.DataBoundItem); categoryBindingSource.RemoveAt(row.Index); } } }
If a checkbox (colRightSelected
) is checked, the corresponding row is added to categoryBindingSource
(which is bound to the dataGridViewLeft
), and removed from categoryBindingSource1
(which is bound to the dataGridViewRight
).
The categoryBindingSource
and categoryBindingSource1
are used to bind the data to the two DataGridView
controls. As data is copied from one DataGridView to another, these bindings ensure that the data is reflected visually in the UI.
categoryBindingSource.Add((Category)row.DataBoundItem); categoryBindingSource1.RemoveAt(row.Index);
C# DataGridView Selected Row
If you want to select a row to copy, you can modify your code based on the selected row.
In C#, you can access the selected row in a DataGridView
by using the SelectedRows
property for multiple row selection or the CurrentRow
property for single row selection.
Accessing a Single Selected Row
If your DataGridView
allows only a single row to be selected at a time, you can use the CurrentRow
property:
// Get the current selected row DataGridViewRow selectedRow = dataGridView1.CurrentRow; // Accessing a cell value from the selected row string value = selectedRow.Cells["ColumnName"].Value.ToString();
Accessing Multiple Selected Rows
If multiple rows can be selected, you can loop through the SelectedRows
collection:
// Loop through selected rows foreach (DataGridViewRow selectedRow in dataGridView1.SelectedRows) { // Accessing a cell value from the selected row string value = selectedRow.Cells["ColumnName"].Value.ToString(); }
Getting the Selected Row Index
If you want to get the index of the selected row, you can use the SelectedRows
property or CurrentRow
:
// Get the index of the selected row int selectedIndex = dataGridView1.CurrentCell.RowIndex; Console.WriteLine($"Selected Row Index: {selectedIndex}");
Handling the Row Selection Changed Event
If you need to perform actions when the selection changes, you can handle the SelectionChanged
event of the DataGridView
:
private void dataGridView1_SelectionChanged(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { // Get the first selected row DataGridViewRow selectedRow = dataGridView1.SelectedRows[0]; string value = selectedRow.Cells["ColumnName"].Value.ToString(); Console.WriteLine(value); } }
This example demonstrates how to copy data between two DataGridView
controls in a Windows Forms application. By utilizing data binding with BindingSource
, you can efficiently manage and display data, and create a user-friendly interface for managing data transfer.
VIDEO TUTORIAL