0

我有一个 DataGridView 对象,我绑定了从数据库查询返回的对象列表(资产类型)。

我正在使用 Visual Studio 2005 在 VB 中编程。

我想从 DataGridView 中的选定行中获取绑定对象的两个副本(称为 oldAsset 和 newAsset),根据表单上其他控件的输入更新 newAsset,并将 oldAsset 和 newAsset 都传递给将更新的函数数据库中的适当记录。

我尝试像这样抓取两个副本:

Dim currentRow As DataGridViewRow = Me.AssetDataGridView.CurrentRow
Dim newAsset As Asset
newAsset = currentRow.DataBoundItem
Dim oldAsset As Asset
oldAsset = currentRow.DataBoundItem

在 oldAsset 和 newAsset 上打开监视窗口表明此时已提取了适当的值。但是当我尝试更改 newAsset 的属性时,例如

newAsset.CurrentLocationID = cboLocations.SelectedValue

我看到 oldAsset 中的相应值也发生了变化。这不是我想要的,但这显然是我告诉计算机要做的。

我如何告诉计算机做我想做的事?

提前致谢!

4

1 回答 1

1

发现出了什么问题。根本不是数据绑定。

newAsset 和 oldAsset 是浅拷贝。我想要深拷贝。

我实现了 ICloneable,编写了一个 Clone() 函数来进行成员复制,并编写了

    Dim oldAsset As Asset
    oldAsset = currentRow.DataBoundItem
    Dim newAsset As Asset = oldAsset.Clone()
于 2009-05-19T20:29:52.273 回答