Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more: , +
Hi All,
How we can copy one or more than one rows from a data grid view to other data grid view on same form. Here, I've searching option which search from the first grid and if found some matching rows then copy them to 2nd grid. How I can achieve this ?
Thanks
Posted
Updated 27-Dec-17 6:11am

VB.NET
For Each rw As DataGridViewRow In dg.Rows
           Dim i As Integer = dg1.Rows.Add(TryCast(rw.Clone, DataGridView))
           For Each cl As DataGridViewCell In rw.Cells
               dg1.Rows(i).Cells(cl.ColumnIndex).Value = cl.Value
           Next
       Next
 
Share this answer
 
Comments
Member 13563129 10-Dec-17 0:27am    
Is this c# code or vb code?
You need to make a loop on source gridview and then check each row and select which one you want to copy then with the help of 'Rows.Add' method we can add rows to destination gridview
see below snippet
C#
DataGridViewRow row = new DataGridViewRow();

       for (int i = 0; i < sourceGRD1.Rows.Count; i++)
       {
           row = (DataGridViewRow)sourceGRD1.Rows[i].Clone();
           int intColIndex = 0;
           foreach (DataGridViewCell cell in sourceGRD1.Rows[i].Cells)
           {
               row.Cells[intColIndex].Value = cell.Value;
               intColIndex++;
           }
           destnationGRD1.Rows.Add(row);
       }
       destnationGRD1.AllowUserToAddRows = false;
       destnationGRD1.Refresh();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900