从 DataGridView 制作数据绑定对象的多个副本 - 如何解耦它们?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现出了什么问题。 根本不是数据绑定。
newAsset 和 oldAsset 是浅拷贝。 我想要深拷贝。
我实现了 ICloneable,编写了一个 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