【问题标题】:DataGridView not updateing when I'm changing datasource更改数据源时 DataGridView 未更新
【发布时间】:2014-09-04 13:41:39
【问题描述】:

我有 DataGridView 和 DataSource 是列表。当我在 gridview 中更改该列表中的元素属性时,它显示在前面。当我单击行时,它会更改值。我正在使用 BackgroundWorker 更新列表。如何同时更新DataGridView?

【问题讨论】:

  • ObservableCollection 代替 List?
  • 列表<_input> oldList;公共类_输入{公共字符串标题{获取;放; } 公共字符串项目 { 获取;放; } 公共双倍成本 { 得到;放; } 公共双固定价格 { 得到;放; } 公共双 FloorPrice { 得到;放; } 公共字符串 Asin1 { 获取;放; } 公共布尔编辑 { 获取;放; } }
  • 你能试试吗,System.Collections.ObjectModel.ObservableCollection<_input> oldList 而不是 List<_input> oldList?
  • 好的。谢谢。我会试试的。
  • 我试过了,但没有用。

标签: c# winforms datagridview


【解决方案1】:

您必须将 ObservableCollection 与 INotifyPropertyChanged 结合使用,因为 ObservableCollection 仅在添加或删除项目时通知,而不是在更改项目时通知。

ObservableCollection Class

How to Listen to Property Changes of Items of an ObservableCollection

SO 上还有一个类似的question 可能会对您有所帮助。

【讨论】:

    【解决方案2】:

    来自msdn的修改摘录

    private void RefreshGrid(object dataSource)
    {
        yourGridName.Invoke((Action)delegate
        {
            var myCurrencyManager = (CurrencyManager)yourGridName.BindingContext[dataSource];
            myCurrencyManager.Refresh();
        });
    }
    

    每当您的后台工作人员更新数据源时调用此方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-27
      • 2013-08-30
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多