我有一个使用实体框架查询数据库并将结果放在ICollectionView
. ICollectionView
充当ItemsSource
a的DataGrid
。在第一次查询时一切正常,但在第二次查询时,尽管应用了正确的SortDescriptions
.
这是我尝试查询和分组/排序数据的代码:
CollectionViewSource cvsRS;
private ObservableCollection<productorder> rs;
public ObservableCollection<productorder> RS
{
get { return rs; }
set
{
if (rs != value)
{
rs = value;
OnPropertyChanged("RS");
}
}
}
private ICollectionView rsView;
public ICollectionView RSView
{
get { return rsView; }
set
{
if (rsView != value)
{
rsView = value;
OnPropertyChanged("RSView");
}
}
}
public void QueryDatabase()
{
RS = new ObservableCollection<productorder>(DatabaseEntities.productorders.Where(o => o.month.id == CurrentMonth.id));
if (RS != null)
{
cvsRS.Source = RS;
RSView = cvsRS.View;
RSView.GroupDescriptions.Clear();
RSView.GroupDescriptions.Add(new PropertyGroupDescription("producttype.productcategory.name"));
RSView.GroupDescriptions.Add(new PropertyGroupDescription("producttype.name"));
RSView.SortDescriptions.Clear();
RSView.SortDescriptions.Add(new SortDescription("producttype.productcategory.sortorder", ListSortDirection.Ascending));
RSView.SortDescriptions.Add(new SortDescription("client.name", ListSortDirection.Ascending));
RSView.Refresh();
CurrentRecord = null;
SelectedRecords = null;
}
}
分组工作正常,但根据排序,组的顺序不正确。我已经尝试了许多可能的“修复”但没有成功(例如,将排序/组描述直接添加到CollectionViewSource
,在分组之前排序,删除一些排序/分组,删除SortDescriptions
每个CollectionViewSource 不会在属性更改时重新排序) .
无论执行多少查询,有谁知道如何维护排序顺序?我愿意接受在DataGrid
可能有效的情况下查询显示数据的替代方法。