我上课有财产,我使用INotifyPropertyChanged
来检测更新。
public class Employee : INotifyPropertyChanged
{
string _lastname;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
public string LastName { get { return _lastName; } set { _lastName = value; NotifyPropertyChanged("LastName"); } }
public Employee(string lastName)
{
LastName = lastName;
}
}
我将它绑定到DataGriView并且它正常工作,LastName自动更新,如果更改。 但如果我试图这样做:
BindingList<Employee> employees = new BindingList<Employee>();
private void BindMain()
{
employees.Add(new Employee("Mike"));
employees.Add(new Employee("Mike"));
employees.Add(new Employee("Joo"));
employees.Add(new Employee("Kate"));
dataGridView.DataSource = employees; // That's working fine, all data auto update
}
private void BindFiltered()
{
List<Employee> filtered = employees.Where(r=>r.LastName.Equals("Mike").ToList()); //
BindingList<Employee> filteredEmployee = new BindingList<Employee>(filtered);
dataGridView.DataSource = filteredEmployee; //not working auto update
// Trying also with BindingSource
BindingSource bs = new BindingSource();
bs.DataSource = employees.Where(r=>r.LastName.Equals("Mike"));
dataGridView.DataSource = bs;
}
..自动更新无效。