看了許多但還是不太懂所以看著msdn實作了一次
1.先新增一個class
public int id { get; set; }
public string name { get; set; }
2.將class繼承INotifyPropertyChanged
using System.ComponentModel;
public class member : INotifyPropertyChanged
3.實作INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
4.修改class
public int id { get; set; }
private string _name;
public string name
{
get { return this._name; }
set
{
if (value != this._name)
{
this._name = value;
NotifyPropertyChanged("Name");
}
}
}
5.新增一個list(抓假資料如果是資料庫也可直接改)
public List6.抓出資料GetMember(int pCount) { List lst = new List (); string name = "test"; for (int i = 0; i < pCount; i++) { member create = new member() { id = i, name = name + i.ToString() }; lst.Add(create); } return lst; }
member script = new member(); List7.寫另一個頁面去修改namelst = script.GetMember(10); dataGridView1.DataSource = lst;
會發覺改了後dataGridview1的name也會跟著改
太神奇又好用了
參考網址
HOW TO:實作 INotifyPropertyChanged 介面
範例下載
INotifyPropertyChanged實作