从 DataGridView 制作数据绑定对象的多个副本 - 如何解耦它们?

发布于 2024-07-19 13:46:51 字数 738 浏览 6 评论 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 中的相应值也发生了更改。 这不是我想要的,但这显然是我告诉计算机要做的事情。

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

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-07-26 13:46:51

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

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

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

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文