Making multiple copies of a data bound object from a DataGridView - how to split them?
I have a DataGridView object to which I have bound a list of objects (of type Asset) returned from a database query.
I am programming in VB using Visual Studio 2005.
I want to grab two copies of a bound object (by calling them oldAsset and newAsset) from the selected row in the DataGridView, update newAsset based on input from other controls on the form, and pass both oldAsset and newAsset to a function that will update the corresponding record in the DB.
I am trying to grab two copies like this:
Dim currentRow As DataGridViewRow = Me.AssetDataGridView.CurrentRow
Dim newAsset As Asset
newAsset = currentRow.DataBoundItem
Dim oldAsset As Asset
oldAsset = currentRow.DataBoundItem
Opening the viewport to oldAsset and newAsset indicates that the corresponding values will be pulled at this point. But when I try to change the property only newAsset like
newAsset.CurrentLocationID = cboLocations.SelectedValue
I can see that the corresponding value in oldAsset changes as well. This is not what I want, but it is obviously what I am telling the computer.
How can I tell the computer to do what I want?
Thanks in advance!
a source to share
Found out what happened. There was no data binding at all.
newAsset and oldAsset were shallow copies. I need deep copies.
I implemented ICloneable, wrote a Clone () function that made a copy in order, and wrote
Dim oldAsset As Asset
oldAsset = currentRow.DataBoundItem
Dim newAsset As Asset = oldAsset.Clone()
a source to share