【问题标题】:Updating of BindingSource in WinForms更新 WinForms 中的 BindingSource
【发布时间】:2012-11-06 20:06:48
【问题描述】:

在我尝试在 Winforms 中使用 DataBinding 期间,我遇到了一个问题。看起来更新 DataSource DataGridView 后不会刷新数据。不明白哪里出了问题。

var companies = new List<Company> { new Company { Name = "Test", Id = 100 }}

这里是项目列表绑定到DataGridView的代码:

bindingSource1.DataSource = _context.Companies;
dataGridView1.DataSource = bindingSource1.DataSource;

但是在那之后,如果我像这样更新companies 列表

 companies.Add(new Company { Name = "MDG", Id = 500 });

我在 DataGridView 中找不到新添加的项目。有人可以帮助我了解我缺少什么吗?

【问题讨论】:

    标签: winforms data-binding datasource bindingsource


    【解决方案1】:

    这里的问题是无法让 BindingSource 和 DataGridView 自动知道对 List 的更改。

    改为使用新的 BindingList()。这具有将被调用以通知 BindingSource 的事件,进而通知 DataGridView 已将新项目添加到列表中。

    var companies = new BindingList<Company>();
    companies.Add(new Company { Name = "Test", Id = 100 });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      相关资源
      最近更新 更多