以编程方式设置数据绑定文本框的文本 [英] Setting text programmatically of a databound textbox

查看:69
本文介绍了以编程方式设置数据绑定文本框的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到BindingSource对象并使用客户对象作为数据源的TextBox。
当我以编程方式更改TextBox的文本时,我发现了一个奇怪的行为,它没有更新更改文本后绑定的对象。

I've a TextBox which is bound to a BindingSource object and use a customer object as the datasource.
I found a strange behavior when I change the text of the TextBox programmatically, it does not update the object bound behind after text is changed.

客户客户=新客户;
customer.CustomerNo =" 123" ;;
bindingSource1.DataSource = customer;
textBox1 .DataBindings.Add(" Text",bindingSource1," CustomerNo");
MessageBox.Show(customer.CustomerNo); // display" 123"
textBox1.Text =" ABC" ;;
MessageBox.Show(customer.CustomerNo); // display" 123"

Customer customer = new Customer;
customer.CustomerNo = "123";
bindingSource1.DataSource = customer;
textBox1.DataBindings.Add("Text", bindingSource1, "CustomerNo");
MessageBox.Show(customer.CustomerNo); // display "123"
textBox1.Text = "ABC";
MessageBox.Show(customer.CustomerNo); // display "123" too

然而,如果我关注TextBox并离开它,它将更新新值"ABC"。对象。
我想知道如果我不应该以编程方式更改任何数据绑定控件的文本?

However, if I focus the TextBox and leave it, it will update new value "ABC" to the object.
I want to know that if I should not change the text of any databound control programmatically?

推荐答案

有你试过bindingSource1.EndEdit()?

Have you tried bindingSource1.EndEdit() ?


这篇关于以编程方式设置数据绑定文本框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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