简体   繁体   English

更改数据源时未更新 C# DataGridView

[英]C# DataGridView not updated when datasource is changed

I have a list of object我有一个对象列表

List<MobilePhone> results;

so i added the list to the datagridview所以我将列表添加到 datagridview

dataGridView.DataSource = phase3Results;

so i have a few dropdown boxes which dictate the list results at any change of selected item in the dropdown boxes, so my list results changes, but on the datagridview its not reflected.所以我有几个下拉框,它们指示下拉框中所选项目的任何更改时的列表结果,所以我的列表结果发生了变化,但在 datagridview 上它没有反映。 is there any way to "refresh" the changes?有没有办法“刷新”更改?

Quick and dirty solution :快速而肮脏的解决方案

dataGridView.DataSource = null;
dataGridView.DataSource = phase3Results;

Clean and correct solution :清洁和正确的解决方案

Use a BindingList<T> instead of List<T> as your DataSource.使用BindingList<T>而不是List<T>作为您的数据源。 List<T> does not fire events when its collection changes. List<T>在其集合更改时不会触发事件。

Also, if you additionally implement INotifyPropertyChanged for T , BindingList<T> automatically subscribes to property changes for each T in the collection and lets the view know about the change.此外,如果您另外为T实现INotifyPropertyChangedBindingList<T>会自动订阅集合中每个T的属性更改,并让视图知道更改。

Try using a BindingList<> instead of List<> and (as already suggested by Daniel), implement INotifyPropertyChanged.尝试使用 BindingList<> 而不是 List<> 并且(正如 Daniel 已经建议的那样),实现 INotifyPropertyChanged。 However, I think you can also call .Refesh() if you didn't want to implement the INotifyPropertyChanged interface.但是,如果您不想实现 INotifyPropertyChanged 接口,我认为您也可以调用 .Refesh() 。

Here's an example ripped from here这是一个从这里撕下来的例子

public class Car : INotifyPropertyChanged
 {
   private string _make;
   private string _model;
   private int _year;

  public event PropertyChangedEventHandler PropertyChanged;

  public Car(string make, string model, int year)
   {
     _make = make;
     _model = model;
     _year = year;
   }

  public string Make
   {
     get { return _make; }
     set
     {
       _make = value;
       this.NotifyPropertyChanged("Make");
     }
   }

  public string Model
   {
     get { return _model; }
     set
     {
       _model = value;
       this.NotifyPropertyChanged("Model");
     }
   }

  public int Year
   {
     get { return _year; }
     set
     {
       _year = value;
       this.NotifyPropertyChanged("Year");
     }
   }

  private void NotifyPropertyChanged(string name)
   {
     if(PropertyChanged != null)
       PropertyChanged(this, new PropertyChangedEventArgs(name));
   }
 }

_dgCars.AutoGenerateColumns = false;

DataGridViewTextBoxColumn makeColumn = new DataGridViewTextBoxColumn();
 makeColumn.DataPropertyName = "Make";
 makeColumn.HeaderText = "The Car's Make";

DataGridViewTextBoxColumn modelColumn = new DataGridViewTextBoxColumn();
 modelColumn.DataPropertyName = "Model";
 modelColumn.HeaderText = "The Car's Model";

DataGridViewTextBoxColumn yearColumn = new DataGridViewTextBoxColumn();
 yearColumn.DataPropertyName = "Year";
 yearColumn.HeaderText = "The Car's Year";

_dgCars.Columns.Add(makeColumn);
 _dgCars.Columns.Add(modelColumn);
 _dgCars.Columns.Add(yearColumn);

BindingList<Car> cars = new BindingList<Car>();

cars.Add(new Car("Ford", "Mustang", 1967));
 cars.Add(new Car("Shelby AC", "Cobra", 1965));
 cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));

_dgCars.DataSource = cars;

You need to implement the INotifyPropertyChanged interface on the object that is storing the data.您需要在存储数据的对象上实现 INotifyPropertyChanged 接口。 Each property needs to raise that event during the set call of a property if the value changed.如果值更改,每个属性都需要在属性的 set 调用期间引发该事件。 Then the grid will automatically get the update.然后网格会自动获取更新。

One easy way is to use new BindingSource(object dataSource, "")一种简单的方法是使用 new BindingSource(object dataSource, "")

This will update the binding source and thus will update the table这将更新绑定源,从而更新表

For example:例如:

dataGridView.DataSource = new BindingSource(phase3Results, "");

As Suggested by Chris Gessler and Daniel, you have to use BindingList<> instead of List<>, and your model should properly implement INotifyPropertyChanged .正如 Chris Gessler 和 Daniel 所建议的那样,您必须使用BindingList<>而不是 List<>,并且您的模型应该正确实现INotifyPropertyChanged But more important, check that your model is actually a Class .但更重要的是,检查您的模型是否实际上是一个 Class Datagridview will discard changes when your model is Records or Struct.当您的模型是 Records 或 Struct 时,Datagridview 将丢弃更改。

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

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