0

I want deepcopy of DataGridViewRows. I have an DataGridViewRow array generated by DataGridView.Rows.CopyTo() function. Is the array dgvra still reference to the same memory as items in DataGridView.Rows?

DGV_Comp is a DataGridView Control

DataGridViewRow[] dgvra = new DataGridViewRow[DGV_Comp.Rows.Count];
DGV_Comp.Rows.CopyTo(dgvra, 0);

From Variable watch, this is what I found

1. Both equivalent cells are 1.357

2. Chanage cell in DGV_Comp to 2.468===>Counterpart cell in dgvra also changed to 2.468

3. Chanage cell in DGV_Comp to 3.579===>Counterpart cell in dgvra also changed to 3.579

4. Clear all rows in DGV_Comp ===> Cell in dgvra changed backed to 1.357

Can anyone help to get a reasonable explanation for what I found?

Thanks in advance.

Stan
  • 1
  • 2
  • Why does it matter? C# is a managed language, for the most part it should not matter if it is the same memory location or not, that is an implementation detail of the runtime. – Minijack Jan 07 '20 at 04:37
  • `object.GetHashCode()` should tell you if it is a reference to the same object (i.e. if the HashCode is the same) – Minijack Jan 07 '20 at 04:38
  • 1
    `DataGridView` should only be display mechanism. Keep data in `DataTable` and there you can copy, etc – T.S. Jan 07 '20 at 04:44
  • It is not a deep copy, the array elements still reference the same row objects. Get ahead with DataGridViewRow.Clone(). – Hans Passant Jan 07 '20 at 10:32

0 Answers0