【问题标题】:How to reorder CollectionViewSource like this?如何像这样重新排序 CollectionViewSource?
【发布时间】:2016-05-06 05:58:10
【问题描述】:

我刚刚得到了一些这样的数据

{
    "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 的模型将始终位于 此列表的顶部

所以当我向我的用户显示这个列表时,他们总是会首先看到MyModelsLOVEU

谢谢!

【问题讨论】:

    标签: c# wpf datagridview collectionviewsource


    【解决方案1】:

    你可以试试这个

     var order = yourCollection.OrderByDescending(y => y.ItemA.IndexOf("LOVEU"));
    

    这将根据属性名称进行排序,这里是它的Name,默认情况下会按升序排序

    编辑: 请在此处查看完整代码dotnetfiddle

    【讨论】:

    • 谢谢,我试过了,但 SortDescription 不能满足我的需要,即总是将带有“LOVEU”的项目放在首位
    • 这个你不需要collection source,直接绑定order这个集合到你控制。如您所见,它将被订购。请参阅dotnetfiddle,以便了解一下
    【解决方案2】:

    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

    【讨论】:

    • 嘿,当我尝试在我的控制台中破坏它时,LOVEU 已排在最后。
    • 谢谢,但是当我单击了一个名为“排序”的按钮时,此更改无法显示在我的 Datagridview 中。
    • 嗨,Eldho 我修好了,我错过了下降,无论如何,如果你想,再试一次
    【解决方案3】:

    给你的另一个建议是,使用事件排序并修改顺序。

     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;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-14
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多