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

25 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
    2. same problem, please im interested too in the final solution

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

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

    ReplyDelete
  10. copying data from dtglist(datagridview1) to another dtgitem(datagridview2)...dtgitem is located in other form...vs2010

    ReplyDelete
  11. hi, i don't know how to binding datagridview from a function , but i change the code to procedure (private DataGridView CopyDataGridView(DataGridView dgv_org) to private void CopyDataGridView(DataGridView dgv_org)), and replace dgv_copy with my second dgv, and call that procedure in rowsadded event from the first dgv, and its work

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

    ReplyDelete
  13. 'Hope This helps DGV1 is the datagridview
    'To copy Row
    Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    End Sub

    'To Paste Row
    Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
    PasteRowIndex = DGV1.CurrentRow.Index
    For index As Int32 = 0 To DGV1.ColumnCount - 1
    DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next

    End Sub

    'To Duplicate Rows
    Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    DGV1.Rows.Add()
    DuplicateRowIndex = DGV1.Rows.Count - 1
    For index As Int32 = 0 To DGV1.ColumnCount - 1
    DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next
    End Sub

    ReplyDelete
  14. Guys I have lot of procedures for copy, duplicate, delete etc. First I need to know if you want to work with 2 data grid views or want to do these functions in the same datagrid view. I can send you the code for the same.

    ReplyDelete
  15. Hi Hanish

    C# How to Copy a Selected Row From one GridView to another GridView With Mouse Click ?

    ReplyDelete