vb.net 如何在通过代码更改值后强制更新 TextBox 的数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21049825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:25:16  来源:igfitidea点击:

How can I force a TextBox's DataSource to update after a change in value through code?

vb.netdata-bindingcontrolsdatasource

提问by Sloth Armstrong

I am binding a TextBox's textproperty to a field value in a DataTableas such:

我将 aTextBoxtext属性绑定到 a 中的字段值,DataTable如下所示:

With control.Item_Full_Description
     .DataBindings.Add("Text", mdtItemMstr, "Item_Full_Description", True)
End With

When I make changes to the TextBoxmanually they are detected and updated in the data source, thus a save to the database is accurate. If I make the change behind the scenes, however, the data source is not updated and a save to the database results in the old value being saved back. Here is how I am updating the TextBoxin the code:

当我TextBox手动更改它们时,它们会在数据源中检测和更新,因此保存到数据库是准确的。但是,如果我在幕后进行更改,数据源不会更新,并且保存到数据库会导致旧值被保存回来。这是我TextBox在代码中更新的方式:

Item_Full_Description.Text = _mItem_mstr_c + " - " + Item_Description.Text + " - " + Unit_of_Measurement_UOM.Text

I am clearly missing a fundamental step to have this change reflected in the data source, however a search online is not proving to be useful. Maybe I am just uncertain of what to search so any advice or even guidance would be appreciated. Thanks.

我显然缺少在数据源中反映此更改的基本步骤,但是事实证明在线搜索没有用。也许我只是不确定要搜索什么,所以任何建议甚至指导都将不胜感激。谢谢。

采纳答案by xspydr

Since you are updating the control from code you need to force the binding source to update.

由于您是从代码更新控件,因此您需要强制更新绑定源。

Look into the BindingContext class... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext%28v=vs.110%29.aspx

查看 BindingContext 类... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext%28v=vs.110%29.aspx

BindingContext([data source]).EndCurrentEdit()