first see the below code which works fine and when we update entity then change is automatically reflected in grid but the moment i comment thisINotifyPropertyChanged and all other code related to notify property change then grid is not getting the change.
so i like to know what kind of role is played by INotifyPropertyChanged ? howINotifyPropertyChanged communicate with grid to show or reflect the chnage? please help me to understand the communication betweengrid and INotifyPropertyChanged . thanks
namespace PatternSearch{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}privatevoid btnBindData_Click(object sender,EventArgs e){BindingList<Car> cars =newBindingList<Car>();
cars.Add(newCar("Ford","Mustang",1967));
cars.Add(newCar("Shelby AC","Cobra",1965));
cars.Add(newCar("Chevrolet","Corvette Sting Ray",1965));
dataGridView1.DataSource= cars;}privatevoid btnUpdate_Click(object sender,EventArgs e){if(dataGridView1.DataSource!=null){BindingList<Car> cars = dataGridView1.DataSourceasBindingList<Car>;
cars.Where(d => d.Make=="Ford").First().Make="My Ford000";}elseMessageBox.Show("Grid has no data");}}publicclassCar:INotifyPropertyChanged{privatestring _make;privatestring _model;privateint _year;publiceventPropertyChangedEventHandlerPropertyChanged;publicCar(string make,string model,int year){
_make = make;
_model = model;
_year = year;}publicstringMake{
get {return _make;}set{
_make = value;this.NotifyPropertyChanged("Make");}}publicstringModel{
get {return _model;}set{
_model = value;this.NotifyPropertyChanged("Model");}}publicintYear{
get {return _year;}set{
_year = value;this.NotifyPropertyChanged("Year");}}privatevoidNotifyPropertyChanged(string name){if(PropertyChanged!=null)PropertyChanged(this,newPropertyChangedEventArgs(name));}}}here is the code where i comment INotifyPropertyChanged and all its related code
namespace PatternSearch{publicpartialclassForm2:Form{publicForm2(){InitializeComponent();}privatevoid btnBindData_Click(object sender,EventArgs e){BindingList<Car> cars =newBindingList<Car>();
cars.Add(newCar("Ford","Mustang",1967));
cars.Add(newCar("Shelby AC","Cobra",1965));
cars.Add(newCar("Chevrolet","Corvette Sting Ray",1965));
dataGridView1.DataSource= cars;}privatevoid btnUpdate_Click(object sender,EventArgs e){if(dataGridView1.DataSource!=null){BindingList<Car> cars = dataGridView1.DataSourceasBindingList<Car>;
cars.Where(d => d.Make=="Ford").First().Make="My Ford000";}elseMessageBox.Show("Grid has no data");}}publicclassCar//: INotifyPropertyChanged{privatestring _make;privatestring _model;privateint _year;//public event PropertyChangedEventHandler PropertyChanged;publicCar(string make,string model,int year){
_make = make;
_model = model;
_year = year;}publicstringMake{
get {return _make;}set{
_make = value;//this.NotifyPropertyChanged("Make");}}publicstringModel{
get {return _model;}set{
_model = value;//this.NotifyPropertyChanged("Model");}}publicintYear{
get {return _year;}set{
_year = value;//this.NotifyPropertyChanged("Year");}}privatevoidNotifyPropertyChanged(string name){//if (PropertyChanged != null)// PropertyChanged(this, new PropertyChangedEventArgs(name));}}}