Click here to Skip to main content
14,386,783 members
Rate this:
Please Sign up or sign in to vote.
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 7:11am
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
Comments
Member 13563129 10-Dec-17 0:27am
   
Is this c# code or vb code?
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
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();
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100