简体   繁体   English

如何刷新C#中数据绑定的表单?

[英]How to refresh a form that is Data Bound in C#?

I have a list box that is data bound to a database and shows firstNames, I have a button that opens a new form and allows the user to edit said firstNames and then saves the changes back to the database(which works fine). 我有一个列表框,该列表框是绑定到数据库的数据并显示firstNames,我有一个按钮可打开一个新表单,并允许用户编辑所说的firstNames,然后将更改保存回数据库(可以正常工作)。 My question is how do I update or refresh the list box in the first form, so that is shows the changes made(as of right now I have to close the application and reboot it to show changes made)? 我的问题是如何更新或刷新第一种形式的列表框,以显示所做的更改(截至目前,我必须关闭应用程序并重新启动以显示所做的更改)?

Note: I am using Microsoft Visual Studio 2010 and created the forms using Windows Forms. 注意:我使用的是Microsoft Visual Studio 2010,并使用Windows窗体创建了窗体。

Write Select query in Form_Load and call after where ever you need to refresh your form like: 在Form_Load中编写Select查询,然后在需要刷新表单的地方调用,例如:

form_load(Object sender,Event_args e)
{
    select Query to display on Loading form
}

add_click(Object sender,Event_args e)
{
    form_load(sender,e)
}

In general, you need to reload data from a database into databinding source. 通常,您需要将数据库中的数据重新加载到数据绑定源中。 If source does not implement IBindingList , you need manually refresh component: 如果源未实现IBindingList ,则需要手动刷新组件:

// BindingContext - the property of ContainerControl (Form or UserControl)
var currencyManager = (CurrencyManager)BindingContext[listBox.DataSource, listBox.DataMember];
currencyManager.Refresh();

I haven't tried this but.... 我没有尝试过,但是....

"However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound." “但是,可以通过在控件绑定到的BindingContext类的实例上调用SuspendBinding和ResumeBinding方法来强制更新组合框。”

http://msdn.microsoft.com/en-us/library/aa984341%28VS.71%29.aspx http://msdn.microsoft.com/zh-CN/library/aa984341%28VS.71%29.aspx

(It should be the same for a ListBox) (对于ListBox应该是相同的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM