如何像这样重新排序 CollectionViewSource?
How to reorder CollectionViewSource like this?
我刚得到一些这样的数据
{
"ID":2,
"NOTAMRec":"C16-0001",
"DeliverDate":"310827",
"BeginDate":"1601010130",
"ExpireDate":"1606070630",
"Priority":"GG",
"ItemA":"LOL",
"OriginalNotamID":2,
"SelectedNotamColor":null
},
{
"ID":8,
"NOTAMRec":"C16-0004",
"DeliverDate":"230705",
"BeginDate":"1602231505",
"ExpireDate":"1606312359 EST",
"Priority":"GG",
"ItemA":"LOVEU",
"OriginalNotamID":8,
"SelectedNotamColor":null
},
{
"ID":9,
"NOTAMRec":"C16-0005",
"DeliverDate":"240703",
"BeginDate":"1602241502",
"ExpireDate":"1606312359 EST",
"Priority":"GG",
"ItemA":"LOVEU",
"OriginalNotamID":9,
"SelectedNotamColor":null
}
我的模型
public Class MyModel
{
public long ID {get;set;}
public string NOTAMRec {get;set;}
public string ItemA {get;set;}
}
并将其添加到 ObservableCollection 中。
然后new CollectionViewSource().Source = theObservableCollection
问题 - 我想按此顺序对我的 ViewSource 进行排序并将其显示在 DataGridView 中。
无论我将多少个 MyModel 添加到此集合中,ItemA
等于“LOVEU
” 的模型将始终在 这个列表的顶部。
所以当我向用户显示此列表时,他们总是会首先看到 MyModels
和 LOVEU
。
谢谢!
Cagaya Coper 试试这个
var query = from c in theObservableCollection
orderby c.ItemA.Equals("LOVEU") descending , c.ID
select c;
new CollectionViewSource().Source = query
Sorting ObservableCollection by String
你可以试试这个
var order = yourCollection.OrderByDescending(y => y.ItemA.IndexOf("LOVEU"));
This will sort based on the property Name here is its Name
and it will sort by ascending by default
编辑:
请在此处查看完整代码 dotnetfiddle
给你的另一个建议是,使用事件排序并修改顺序。
grid.DataContext = ObservableCollection.OrderByDescending(y => y.ItemA.IndexOf("LOVEU"));
private void grid_Sorting(object sender, DataGridSortingEventArgs e)
{
ListCollectionView view = (ListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext));
if (e.Column != null && e.Column.CanUserSort == true && e.Column.Header.ToString() != "ItemA")
{
var dgSender = (DataGrid)sender;
var cView = CollectionViewSource.GetDefaultView(dgSender.ItemsSource);
cView.SortDescriptions.Clear();
cView.SortDescriptions.Add(new SortDescription("ItemA", ListSortDirection.Descending));
if (e.Column.SortDirection == ListSortDirection.Ascending)
{
e.Column.SortDirection = ListSortDirection.Descending;
cView.SortDescriptions.Add(new SortDescription(e.Column.Header.ToString(), ListSortDirection.Descending));
}
else
{
e.Column.SortDirection = ListSortDirection.Ascending;
cView.SortDescriptions.Add(new SortDescription(e.Column.Header.ToString(), ListSortDirection.Ascending));
}
e.Handled = true;
}
}
我刚得到一些这样的数据
{
"ID":2,
"NOTAMRec":"C16-0001",
"DeliverDate":"310827",
"BeginDate":"1601010130",
"ExpireDate":"1606070630",
"Priority":"GG",
"ItemA":"LOL",
"OriginalNotamID":2,
"SelectedNotamColor":null
},
{
"ID":8,
"NOTAMRec":"C16-0004",
"DeliverDate":"230705",
"BeginDate":"1602231505",
"ExpireDate":"1606312359 EST",
"Priority":"GG",
"ItemA":"LOVEU",
"OriginalNotamID":8,
"SelectedNotamColor":null
},
{
"ID":9,
"NOTAMRec":"C16-0005",
"DeliverDate":"240703",
"BeginDate":"1602241502",
"ExpireDate":"1606312359 EST",
"Priority":"GG",
"ItemA":"LOVEU",
"OriginalNotamID":9,
"SelectedNotamColor":null
}
我的模型
public Class MyModel
{
public long ID {get;set;}
public string NOTAMRec {get;set;}
public string ItemA {get;set;}
}
并将其添加到 ObservableCollection 中。
然后new CollectionViewSource().Source = theObservableCollection
问题 - 我想按此顺序对我的 ViewSource 进行排序并将其显示在 DataGridView 中。
无论我将多少个 MyModel 添加到此集合中,ItemA
等于“LOVEU
” 的模型将始终在 这个列表的顶部。
所以当我向用户显示此列表时,他们总是会首先看到 MyModels
和 LOVEU
。
谢谢!
Cagaya Coper 试试这个
var query = from c in theObservableCollection
orderby c.ItemA.Equals("LOVEU") descending , c.ID
select c;
new CollectionViewSource().Source = query
Sorting ObservableCollection by String
你可以试试这个
var order = yourCollection.OrderByDescending(y => y.ItemA.IndexOf("LOVEU"));
This will sort based on the property Name here is its
Name
and it will sort by ascending by default
编辑: 请在此处查看完整代码 dotnetfiddle
给你的另一个建议是,使用事件排序并修改顺序。
grid.DataContext = ObservableCollection.OrderByDescending(y => y.ItemA.IndexOf("LOVEU"));
private void grid_Sorting(object sender, DataGridSortingEventArgs e)
{
ListCollectionView view = (ListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext));
if (e.Column != null && e.Column.CanUserSort == true && e.Column.Header.ToString() != "ItemA")
{
var dgSender = (DataGrid)sender;
var cView = CollectionViewSource.GetDefaultView(dgSender.ItemsSource);
cView.SortDescriptions.Clear();
cView.SortDescriptions.Add(new SortDescription("ItemA", ListSortDirection.Descending));
if (e.Column.SortDirection == ListSortDirection.Ascending)
{
e.Column.SortDirection = ListSortDirection.Descending;
cView.SortDescriptions.Add(new SortDescription(e.Column.Header.ToString(), ListSortDirection.Descending));
}
else
{
e.Column.SortDirection = ListSortDirection.Ascending;
cView.SortDescriptions.Add(new SortDescription(e.Column.Header.ToString(), ListSortDirection.Ascending));
}
e.Handled = true;
}
}