i was reading an article on two way data binding in winform and i tested their code which works fine. i was not aware about two way data binding in winform. here is article url http://ift.tt/1AR8fUj
please review my code and tell me am i on right direction? is there any other better option exist to achieve the same or complicated situation. just looking for guidance.
here is my full code
namespace PatternSearch
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnBindData_Click(object sender, EventArgs e)
{
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));
dataGridView1.DataSource = cars;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView1.DataSource != null)
{
BindingList<Car> cars = dataGridView1.DataSource as BindingList<Car>;
cars.Where(d => d.Make == "Ford").First().Make = "My Ford000";
}
else
MessageBox.Show("Grid has no data");
}
}
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));
}
}
}
thanks
Aucun commentaire:
Enregistrer un commentaire