【问题标题】:How to refresh a form that is Data Bound in C#?如何刷新 C# 中数据绑定的表单?
【发布时间】:2011-09-18 07:44:42
【问题描述】:

我有一个列表框,它是数据绑定到数据库并显示名字,我有一个按钮可以打开一个新表单并允许用户编辑所述名字,然后将更改保存回数据库(工作正常) .我的问题是如何更新或刷新第一个表单中的列表框,以便显示所做的更改(现在我必须关闭应用程序并重新启动它才能显示所做的更改)?

注意: 我正在使用 Microsoft Visual Studio 2010 并使用 Windows 窗体创建表单。

【问题讨论】:

  • ASP.NET?窗体? WPF?你在用什么?
  • @Kyle Trauberman - Windows 窗体

标签: c# database winforms data-binding refresh


【解决方案1】:

在 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)
}

【讨论】:

    【解决方案2】:

    我没试过,但是....

    “但是,您可以通过在控件绑定到的 BindingContext 类的实例上调用 SuspendBinding 和 ResumeBinding 方法来强制更新组合框。”

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

    (ListBox应该一样)

    【讨论】:

      【解决方案3】:

      通常,您需要将数据从数据库重新加载到数据绑定源中。如果source没有实现IBindingList,则需要手动刷新组件:

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

      【讨论】:

      • 好的,我试过了,我得到了这个错误“在数据源上找不到数据成员属性“tblHorse”。”这就是我将其更改为 // BindingContext - ContainerControl (Form 或 UserControl) 的属性 var currencyManager = (CurrencyManager)BindingContext[lstHorseNames.DataSource,tblHorseBindingSource.DataMember];currencyManager.Refresh();
      猜你喜欢
      • 2012-02-19
      • 2011-07-21
      • 1970-01-01
      • 2011-01-25
      • 2012-12-27
      • 1970-01-01
      • 2015-07-12
      • 2011-04-13
      • 2023-03-31
      相关资源
      最近更新 更多