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) ?
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.
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
You use the function at the following link
http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html
just write this: