使用CurrencyManager 控制绑定,无需BindingSource

发布于 2024-11-30 07:24:13 字数 1124 浏览 2 评论 0原文

我决定不使用 BindingSource 类,而是在我的 Windows 窗体应用程序上实现绑定功能。我在某些方面取得了成功,但很少出现并发症。我想找出原因。 我有从数据源填充的 DataTable 和三个控件:textbox1、textbox2、checkedBox1。 我设法绑定控件并通过以下方式显示值:

txtBox1.DataBindings.Add("Text", myTable, "NAME", true, DataSourceUpdateMode.Never);

正如您所猜测的,myTable 是包含从数据库表填充的数据的数据表。 我可以使用CurrencyManager获取当前行值,例如;

DataRow dr = ((DataRowView)this.BindingContext[myTable].Current).Row;

但是,当我想使用当前行更改值时,在第一个字段设置之后,其他字段会自动更改为之前的值。我的意思是:

dr["NAME"]= textBox1.Text;


dr["SURNAME"] = textBox2.Text;  //this set seems useless

第一个赋值有效,但随后表单控件在编辑开始之前将 textBox2、checkBox1 的值更改回原始值。所以我无法以这种方式更新整行。
另一方面,通过使用 BindingSource,我将获得当前行类似的样式:

DataRow drB = ((DataRowView)bindingSource1.Current).Row;

并通过以下方式更改该行的字段:

drB["NAME"] = textBox1.Text;
drB["SURNAME"] = textBox2.Text;
drB["ACTIVE"] = checkBox1.Checked;

结束编辑时:

bindingSource1.EndEdit();

所有表已准备好更新,因为整行已成功更改。 我想看到差异,并在不使用绑定源的情况下管理它。 这是否意味着我应该使用 EndEdit() 所做的事情? 我错过了什么,或者忘记了痴迷?

I decided not to use bindingSource class but implement binding functionality on my windows form application. I succeed to some points but little complication occurs. I would like to find out the reason.
I have DataTable filled from datasource and three controls: textbox1, textbox2, checkedBox1.
I managed to bind the controls and show the values by;

txtBox1.DataBindings.Add("Text", myTable, "NAME", true, DataSourceUpdateMode.Never);

As you guess, myTable is the datatable contains the data filled from database table.
I can get the current row values with CurrencyManager, such as;

DataRow dr = ((DataRowView)this.BindingContext[myTable].Current).Row;

But when i want to change the values using current row, somehow after first field set, others automatically changes to prior values. I mean:

dr["NAME"]= textBox1.Text;


dr["SURNAME"] = textBox2.Text;  //this set seems useless

First assignment works but then form controls that textBox2, checkBox1 changes their values back to original before the editing begins. So i can not update the full row in this way.
On the other hand, with using BindingSource i would get the current row similar style:

DataRow drB = ((DataRowView)bindingSource1.Current).Row;

And change the fields of this row by:

drB["NAME"] = textBox1.Text;
drB["SURNAME"] = textBox2.Text;
drB["ACTIVE"] = checkBox1.Checked;

with ending the edit with:

bindingSource1.EndEdit();

All table is ready to update because whole row changed successfully.
I would like to see the difference, and manage it without using bindingSource.
Does it mean that I should use something EndEdit() does?
What am I missing, or forget the obsession?

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

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

发布评论

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

评论(2

橘虞初梦 2024-12-07 07:24:13

第一次更改 (dr["NAME"]= textBox1.Text;) 后,从绑定中引发 OnPropertyChanged/OnListChanged。第二个文本框和复选框侦听此消息并也在更新这些值,但这些值是旧值。

After the first change (dr["NAME"]= textBox1.Text;) OnPropertyChanged/OnListChanged is raised from the binding. the second textbox and the checkbox listen to this message and are updating there values too, but these values are the old ones.

弄潮 2024-12-07 07:24:13

正确的答案已经显现,因为我错过了一点提示。那是在直接从控制值更改数据行字段之前使用临时变量。因此,即使控制值更改回原始值,我们也可以再次实现更改整个行字段,从而最终将控制值更改为新值。也就是说:

string s1 = textBox1.Text;
string s2 = textBox2.Text;
bool b1 = checkBox1.Checked;
dr["NAME"] = s1;
dr["SURNAME"] = s2;
dr["ACTIVE"] = b1;

使用这些临时变量,但是控制值在第一次分配后发生了变化,最后它们全部采用新值。
如果有人需要,祝你好运。

Right answer has shown itself, 'cause i missed a little cue. That was using temp variables before changing the datarow field from the control values directly. So even control values changed back to their orignal values, we could again achive to change whole row fields thus control values to the new values eventually.That is :

string s1 = textBox1.Text;
string s2 = textBox2.Text;
bool b1 = checkBox1.Checked;
dr["NAME"] = s1;
dr["SURNAME"] = s2;
dr["ACTIVE"] = b1;

With these temp variable, however the control values changed after first assignment, at the end they all with the new values.
If someone needs, good luck.

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