我心里想的是,如果我在自定义集合上实现INotifyCollectionChanged,DataGridView就会订阅CollectionChanged事件。
My实现了IListSource和INotifyCollectionChanged,并且有一个内部BindingList。我从ListChanged订阅BindingList事件并调用我的OnCollectionChanged方法,然后该方法引发CollectionChanged事件。
也许有更好的方法来完成以上的工作,我很高兴听到。但是,我目前最关心的是在调用这个排序方法之后让DataGridView更新:
public void Sort(List<SortField> sortFields)
{
if(sortFields == null || sortFields.Count == 0) return;
IOrderedEnumerable<T> res;
if (sortFields[0].Ascending)
res = _items.OrderBy(o => o[sortFields[0].Name]);
else
res = _items.OrderByDescending(o => o[sortFields[0].Name]);
for (int x = 1; x < sortFields.Count; x++)
if (sortFields[x].Ascending)
res = res.ThenBy(o => o[sortFields[x].Name]);
else
res = res.ThenByDescending(o => o[sortFields[x].Name]);
Items = new BindingList<T>(res.ToList());
OnListChanged(this, new ListChangedEventArgs(ListChangedType.Reset, null));
}我是误以为DataGridView订阅了CollectionChanged事件,还是做错了其他事情?
发布于 2014-02-05 08:36:55
我假设您在自定义集合中使用ObservableCollection<T>类。DataGridView不知道INotifyCollectionChanged的事。它用于WPF绑定,而不用于WinForms。
有关更多信息,请参见this。
https://stackoverflow.com/questions/21571437
复制相似问题