Transfer Selected Rows from One GridView to Another GridView
Transfer Selected Rows from One GridView to Another GridView : It’s a simple C# article about how to transfer or move selected gridview rows or records from one gridview to another gridview in ASP.NET using C#.
One of our Code Scratcher’s viewer want some help about the transfer gridview records from one girdview to another. So today’s article for him. Hope! It will very helpful to him and also for others developers.
In previous articles we explained Language Translation using Resource File in WPF, 3 Tier Architecture in ASP.NET, Watermark TextBox, Import Excel to SQL Server using SqlBulkCopy, Add Twitter Widget, Tiled Menu Slider in Flash, Generate and Print Barcode, Auto Rotate Content, 3D Spin Menu, Play Sound in Flash, Simple XML Banner Rotator in flash, Load XML Data in Flash, URL Rewrite, Database operation in WPF with Access Database, Get Directions Google Map API and many more. Now we move on How to transfer selected rows from one gridview to another gridview in ASP.NET using C#.
Following are the steps to transfer selected rows from one gridview to another gridview.
In this article first we apply simple logic that copy selected rows or records from first grid and paste into second gird then after remove that records from first grid.
Create a windows forms application using C#
If you are sound with Windows Forms then you know very well how to create a windows forms application.
LEARN MORE : HOW TO CREATE WINDOWS FORMS APPLICATION IN VISUAL STUDIO 2010
DESIGN WINDOWS FORM
ADD CODE-BEHIND SOURCE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MoveRecordsBetweenTwoGridview { public partial class Form1 : Form { //Define two tables DataTable dt1 = new DataTable("Table1"); DataTable dt2 = new DataTable("Table2"); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { GenenrateTable1(); GenenrateTable2(); AddCheckBox(); GV1.DataSource = dt1; GV2.DataSource = dt2; } public void GenenrateTable1() { //Add Columns in Table1 dt1.Columns.Add("Id").ToString().Trim(); dt1.Columns.Add("Name").ToString().Trim(); //Add rows;records in Table1 for (int i = 1; i < 6; i++) { DataRow dr = dt1.NewRow(); dr["Id"] = "Id" + i; dr["Name"] = "Name" + i; dt1.Rows.Add(dr); } } private void GenenrateTable2() { //Add Columns in Table2 dt2.Columns.Add("Id").ToString().Trim(); dt2.Columns.Add("Name").ToString().Trim(); //Add rows;records in Table2 for (int i = 21; i < 26; i++) { DataRow dr = dt2.NewRow(); dr["Id"] = "Id" + i; dr["Name"] = "Name" + i; dt2.Rows.Add(dr); } } private void AddCheckBox() { //Add checkbox in first datagridview for select records DataGridViewCheckBoxColumn cb1 = new DataGridViewCheckBoxColumn(); cb1.HeaderText = "Select"; cb1.Name = "gv1Select"; GV1.Columns.Add(cb1); //Add checkbox in second datagridview for select records DataGridViewCheckBoxColumn cb2 = new DataGridViewCheckBoxColumn(); cb2.HeaderText = "Select"; cb2.Name = "gv2Select"; GV2.Columns.Add(cb2); } //Transfer selected records from first datagridview to second datagridview private void btnGV1toGV2_Click(object sender, EventArgs e) { string Id = ""; foreach (DataGridViewRow dr in GV1.Rows) { if (Convert.ToBoolean(dr.Cells["gv1Select"].Value) == true) { DataRow gv2dr = dt2.NewRow(); gv2dr["Id"] = dr.Cells["Id"].Value.ToString(); gv2dr["Name"] = dr.Cells["Name"].Value.ToString(); //Add slected rows in second datagridview dt2.Rows.Add(gv2dr); //comma separated selected rows Id += "," + dr.Cells["Id"].Value.ToString(); } } if (Id != "") { //Split Id with ',' and get all rows in array. string[] result = Id.Substring(1).ToString().Split(','); foreach (string IdtoDelete in result) { DataRow[] Dr = null; Dr = dt1.Select("Id = '" + IdtoDelete + "'"); for (int i = 0; i <= Dr.Length - 1; i++) { //Remove all the rows from first datagridview dt1.Rows.Remove(Dr[i]); } } } } //Transfer selected records from second datagridview to first datagridview private void btnGV2toGV1_Click(object sender, EventArgs e) { string Id = ""; foreach (DataGridViewRow dr in GV2.Rows) { if (Convert.ToBoolean(dr.Cells["gv2Select"].Value) == true) { DataRow gv = dt1.NewRow(); gv["Id"] = dr.Cells["Id"].Value.ToString(); gv["Name"] = dr.Cells["Name"].Value.ToString(); //Add slected rows in first datagridview dt1.Rows.Add(gv); //comma separated selected rows Id += "," + dr.Cells["Id"].Value.ToString(); } } if (Id != "") { string[] result = Id.Substring(1).ToString().Split(','); foreach (string IdtoDelete in result) { DataRow[] Dr = null; Dr = dt2.Select("Id = '" + IdtoDelete + "'"); for (int i = 0; i <= Dr.Length - 1; i++) { //Remove all the rows from second datagridview dt2.Rows.Remove(Dr[i]); } } } } } } |
RUN PROJECT AND CHECK FINAL OUTPUT

Transfer Selected Rows from One GridView to Another GridView – Output
Source Code
help@codescratcher.com
Incoming search terms
Transfer Selected Rows from One GridView to Another GridView, Move Selected Gridview Rows to Another Gridview in Asp.net, Transfer Selected Rows from one GridView to Another in Asp.net, How I can add checked rows from grid view to another grid view, Transfer selected (checked) rows from one GridView to another GridView, How Copy Selected row from one Gridview to another in Asp.net using C#, how to transfer user selected rows from one gridview to another, Transfer records from one Gridview to another Gridview in Asp.net, Copy selected row from one GridView to a second Gridview.
Transfer Selected Rows from One GridView to Another GridView
I searched and tried to work on this for my project many times in google but i got it here,
thank you very much.
but can you have the same with sql database.
please guide me how to do this with sql database and c#
please reply
Hi,
You can replace your database table logic at the place of static data table. It’s very simple to implement.
Before that you need to understand how to connect SQL with your project.
Refer this link: http://www.codescratcher.com/asp-net/insert-update-delete-data-database-using-stored-procedure
Regards,
Code Scratcher Team.
Hi, can you please show how to “copy” checked rows from one DGV to other DGV if both DGVs are bound to datasource? In VB?
Kudos! This article is greate