views:

90

answers:

1

I have a DataGridView object to which I've bound a list of objects (of type Asset) returned from a database query.

I'm programming in VB using Visual Studio 2005.

I want to grab two copies of the bound object (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 appropriate record in the DB.

I try to grab the 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 a watch window on oldAsset and newAsset indicates that the appropriate values are pulled at this point. But when I try to change a property of just newAsset, like

newAsset.CurrentLocationID = cboLocations.SelectedValue

I see that the corresponding value in oldAsset is also changed. This is not what I want, but it's obviously what I'm telling the computer to do.

How do I tell the computer to do what I want?

Thanks in advance!

A: 

Found out what was wrong. Wasn't the data binding at all.

newAsset and oldAsset were shallow copies. I wanted deep copies.

I implemented ICloneable, wrote a Clone() function that did the memberwise copy, and wrote

    Dim oldAsset As Asset
    oldAsset = currentRow.DataBoundItem
    Dim newAsset As Asset = oldAsset.Clone()
John at CashCommons