9

So basically I've got 2 DataGridView and I need to copy the rows from one to the other.

So far I've tried:

DataGridViewRowCollection tmpRowCollection = DataGridView1.Rows;

DataGridViewRow[] tmpRowArray = new DataGridViewRow[tmpRowCollection.Count];
tmpRowCollection.CopyTo(tmpRowArray, 0);            

DataGridView2.Rows.AddRange((DataGridViewRow[]) tmpRowArray));

But it keeps saying that

"Row provided already belongs to a DataGridView control."

So what's the best way to copy the content of the rows (both DataGridView have the same columns) ?

YakovL
  • 7,557
  • 12
  • 62
  • 102
VAShhh
  • 3,494
  • 2
  • 24
  • 37

4 Answers4

9

You use the function at the following link

private DataGridView CopyDataGridView(DataGridView dgv_org)
{
    DataGridView dgv_copy = new DataGridView();
    try
    {
        if (dgv_copy.Columns.Count == 0)
        {
            foreach (DataGridViewColumn dgvc in dgv_org.Columns)
            {
                dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
            }
        }

        DataGridViewRow row = new DataGridViewRow();

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

    }
    catch (Exception ex)
    {
        cf.ShowExceptionErrorMsg("Copy DataGridViw", ex);
    }
    return dgv_copy;
}

http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

Tharif
  • 13,794
  • 9
  • 55
  • 77
Wicked Coder
  • 1,128
  • 6
  • 8
1

you need to first clone the row from the original then add to new view. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx

Bueller
  • 2,336
  • 17
  • 11
  • If i do `DataGridView2.Rows.AddRange((DataGridViewRow[]) tmpRowArray.clone()));` I get the same error. Do I have to clone each row in a loop? :| – VAShhh Jun 13 '11 at 21:01
  • unfortunately i think that is the case. however if the original datagridview is bound to a dataset, you can bind the second datagridview to the same or clone of the dataset and get what you are after as well. – Bueller Jun 13 '11 at 21:03
0

just write this:

copyDGV.DataSource = mainDGV.DataSource;        
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44
  • welcome to Stackoverflow. Please format your code properly :) Best regards – YakovL Jul 29 '17 at 12:31
  • @Dr.DEVELOPER: A good try, though it wouldn't be of any use for datagridviews without datasources. – D J Apr 05 '20 at 17:21
0

I would recommend using a backing DTO for this. Instead of dealing with the rows directly, create a DTO that contains all the columns of your GridViews, then use a List of them as your DataSource. Then, all you have to do to add/remove rows is to add/remove DTOs in the list.

KeithS
  • 70,210
  • 21
  • 112
  • 164