このクラスを考えてみましょう:
public class MyClass : INotifyPropertyChanged
{
private int _id;
private string _value;
public int Id
{
get
{
return _id;
}
set
{
PropertyChanged(this, new PropertyChangedEventArgs("Id"));
_id = value;
}
}
public string Value
{
get
{
return _value;
}
set
{
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
_value = value;
}
}
public event PropertyChangedEventHandler PropertyChanged = new PropertyChangedEventHandler(OnPropertyChanged);
private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// optional code logic
}
}
これらのメソッドを並べ替え可能なバインディング リストに追加します。
public class SortableBindingList<T> : BindingList<T>, INotifyPropertyChanged
where T : class
{
public void Add(INotifyPropertyChanged item)
{
item.PropertyChanged += item_PropertyChanged;
base.Add((T)item);
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.PropertyChanged(sender, new PropertyChangedEventArgs(String.Format("{0}:{1}", sender, e)));
}
// other content in the method body
}
そして、このサンプル メソッドをフォームで使用します。
public Form1()
{
InitializeComponent();
source = new SortableBindingList<MyClass>();
source.Add(new MyClass() { Id = 1, Value = "one test" });
source.Add(new MyClass() { Id = 2, Value = "second test" });
source.Add(new MyClass() { Id = 3, Value = "another test" });
source.PropertyChanged += source_PropertyChanged;
dataGridView1.DataSource = source;
}
void source_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
dataGridView1.DataSource = source;
}
private void button1_Click(object sender, EventArgs e)
{
((MyClass)source[0]).Id++;
}