How to Copy a Selected Row From one DataGridView to another DataGridView in C#
By FoxLearn 11/27/2024 9:00:52 AM 12.51K
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);
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