使用INotifyPropertyChanged接口,当表数据更改时发生更新
public class Customer :INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged=delegate{}; private string _customerName; private string _phoneNumber; public string CustomerName { get{return _customerName;} set { if(_customerName!=value) { _customerName=value; PropertyChanged(this,new PropertyChanged("CustomerName")); } } } public string PhoneNumber { get{return _phoneNumber;} set { if(_phoneNumber!=value) { _phoneNumber=value; PropertyChanged(this,new PropertyChanged("PhoneNumber")); } } } } /*******************************************************/ public class Form { private BindingList<Customer> customers= new BindingList<Customer>(); } public void AddData() { customers.Add(new Customer() {Customer="zs",PhoneNumber="123"}); customers.Add(new Customer(){Customer="ls",PhoneNumber="456"}); this.dataGridViewe1.DataSource = customers; } public void UpdateData() { customers[0].CustomerName="ww"; }
原文:https://www.cnblogs.com/clgis/p/10096310.html