Monday, 1 June 2009

Copying a DataGridViewRow to another DataGridView Control

Sometimes a simple task can be very tricky. Today I was trying to make a copy of DataGridView to another DataGridView, in the traget DataGridView I need to delete a few rows and possibly add a few new DataGridViewColumn(s), all these changes must have no impact whatsoever on the Original DataGridView object. Because of the nature of C#, object is passed by reference. So I cannot do things like DataGridView dgv_copy = dgv_original if I want to make some changes in the second DataGridView without affecting the original one.

What should I do? Firstly I need to create columns first, populate the DataGridViewColumns from the original to the new one. This is very straight forward, however when it comes to copy the rows. I came across a problem. The DataGridViewRow clone method does not work as expected. The Clone method copies the row and its property values, but does NOT copy the cell values that the row contains. So we have to write some code to copy the cell values. Please find the working example below and feel free to let me know should you have any queries.

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;
}

14 comments:

  1. great work. Thank you wery much!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. how do you move the copied Row to the another DataGridView

    ReplyDelete
  4. i want to copy only those rows which have been checked in one datagridview to another without including the check box column of the previous datagridview..please help..thanks in advance..

    ReplyDelete
  5. In order to copy only the Selected Row

    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.SelectedRows.Count; i++)
    {
    row = (DataGridViewRow)dgv_org.SelectedRows[i].Clone();
    int intColIndex = 0;
    foreach (DataGridViewCell cell in dgv_org.SelectedRows[i].Cells)
    {
    row.Cells[intColIndex].Value = cell.Value;
    intColIndex++;
    }
    dgv_copy.Rows.Add(row);
    }
    dgv_copy.AllowUserToAddRows = false;
    dgv_copy.Refresh();

    }
    catch (Exception ex)
    {
    System.Console.WriteLine(ex.ToString());

    }
    return dgv_copy;
    }



    }
    }

    ReplyDelete
  6. yes but may i ask where do you copy-paste this code,in the form with original datagrid or the other one? Thx Bane

    ReplyDelete
  7. Hi,

    I'm new at c#, but not at OOP.

    I've tried to implement your code, but I'm not able to see the changes on the copied datagrid. The line I use to copy is like this:

    this.dataGridView2=CopyDataGridView(this.dataGridView1);

    I've tried to call .refresh, .update methods and so on, but the copied datagrid (created in dessign time) continue empty, althoug if I ask on debug immediate window dataGridView2.rowsCount, it returns 9.

    I hope you can help me on this, because I'm actually stucked.

    I wanted to reorder the dg1, but as I've been unable to do it because it's data-bounded, I thought it was possible to do it on a copy...

    Thanks in advance!

    ReplyDelete
    Replies
    1. Hello, Have you solved the problem? I have same problem like what you posted. I am interested in the final solution as well.

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. how binding data. Please me
    this.dataGridView2=CopyDataGridView(this.dataGridView1);

    ReplyDelete