当行被修改时,HasChanges()总是为false,除非我调用窗体的BindingContext EndCurrentEdit()方法 [英] HasChanges() is always false when rows are modified unless I call the form's BindingContext EndCurrentEdit() method

查看:380
本文介绍了当行被修改时,HasChanges()总是为false,除非我调用窗体的BindingContext EndCurrentEdit()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 DataSet ,我填充。为了测试目的,它包含一个 DataTable 。这是从一个查询返回一个SQL TABLE ,它有两列, ID 姓名。这个表有一个记录。

So I have a DataSet, that I populate. For test purposes, this contains a single DataTable. This is populated from a query that returns a SQL TABLE which has two columns, ID and Name. This table has one record in it.

我有一个单独的 TextBox ,我按以下方式绑定:

I have a single TextBox, that I bind in the following manner:

Dim dv As System.Data.DataView = myDataSet.Tables("MyTable").AsDataView()
dv.RowFilter = "ID=1"

Dim dabi As New Binding("Text", dv, "Name") 
txtInput.DataBindings.Add(dabi)

这一切都在应用程序中运行良好! TextBox 使用 DataSet 中的值填充,存储在 DataSet 被更新,因为我在 TextBox 中输入内容。

This all works great in the application! The TextBox populates with the value from the DataSet, and the value stored in the DataSet is updated as I enter stuff into the TextBox.

所以,我做一个 TextBox ,然后我去保存。

So, I make a in the TextBox, and then I go to save it.

我调用 myDataSet.HasChanges ()。即使我做了改变,它返回 False 。研究表明,这与不相信编辑完成的行有关,但我不确定这一点,因为我无法找到一个关于这一切如何工作的良好信息来源。

I call myDataSet.HasChanges(). It returns False, even though I've made a change. Research suggests that this is something to do with the row not believing editing is 'finished' yet, but I'm not certain on this, as I've not been able to find a good source of information on how this is all working.

我还发现 HasChanges()将返回正确的值( False 为没有更改, True 更改)如果我在调用之前调用 myDataSet.HasChanges()以下行:

I have also discovered that HasChanges() will return the correct value (False for no changes, True for changes) if I precede the call to myDataSet.HasChanges() with the following line:

Me.BindingContext(myDataSet.Tables(MyTable))。EndCurrentEdit()

这是一个被称为似乎是某种全局绑定上下文的方法。我没有做任何事情,或者称之为任何开始编辑方法等(研究表明这样的事情可能是私人的,隐含的)。

This is a method being called for what appears to be some sort of global binding context. I've not done anything with this, or called any 'Begin Edit' methods, etc (research suggests such things may be private and called implicitly).

我的问题: 这里发生了什么?为什么 HasChanges()可以使用该行?在我的数据绑定中我做错了吗?

My questions then: What's going on here? Why is that line necessary for HasChanges() to work? Am I doing something wrong in my data-binding to necessitate this line? Does that line have any potentially unforeseen side-effects?

编辑: HasChanges()如果没有调用 EndCurrentEdit()的新行添加,将会正确返回true,但问题只出现在对现有行的修改中。 p>

HasChanges() will correctly return true if new rows are added without the call to EndCurrentEdit(), the issue only arises with modifications to existing rows.

推荐答案

调用 EndCurrentEdit 导致您在 IEditableObject 保存到基础数据源。

Calling EndCurrentEdit cause the changes which you made on an IEditableObject be saved to underlying data source.

由于 DataRowView 实现 IEditableObject ,调用货币管理器的EndCurrentEdit 导致 DataRowView 的rel =nofollow> EndEdit ,并对底层的DataRow进行更改,结束编辑会话。

Since the DataRowView implements IEditableObject, calling EndCurrentEdit of the currency manager cause EndEdit of DataRowView be called and commits changes to the underlying DataRow and ends the editing session.

此外,当您使用绑定源更改职位或通过单击另一行网格更改职位时,如果事实上您正在更改货币管理器的位置,则 EndCurrentEdit 被调用。

Also when you change the position using binding source or change the position by click on another row of grid, if fact you are changing the position of currency manager which case EndCurrentEdit be called.

为了提交更改,您应直接或间接调用 EndCurrentEdit

So to commit changes, you should call EndCurrentEdit directly or indirectly.

这篇关于当行被修改时,HasChanges()总是为false,除非我调用窗体的BindingContext EndCurrentEdit()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆