【问题标题】:Retain the user defined sort order in WPF DataGrid在 WPF DataGrid 中保留用户定义的排序顺序
【发布时间】:2023-03-08 21:07:01
【问题描述】:

我有一个 WPF DataGrid,其中填充了来自 DataSet 的数据。我将 CanUserSortColumns 设置为 true

是否可以保留用户在刷新网格时指定的排序?我让它保留使用

选择的项目
  object selectedItem = dgInvoiceHeads.SelectedItem;

在刷新之前然后放置

 dgInvoiceHeads.SelectedItem = selectedItem;

刷新后。

但我似乎无法让它保留指定的排序。

【问题讨论】:

    标签: c# wpf xaml sorting wpfdatagrid


    【解决方案1】:

    以下代码是从forum post 中提取的,它显示了如何获取排序描述和列信息并恢复它。

    List<DataGridColumn> GetColumnInfo(DataGrid dg) {
        List<DataGridColumn> columnInfos = new List<DataGridColumn>();
        foreach (var column in dg.Columns) {
            columnInfos.Add(column);
        }
        return columnInfos;
    }
    
    List<SortDescription> GetSortInfo(DataGrid dg) {
        List<SortDescription> sortInfos = new List<SortDescription>();
        foreach (var sortDescription in dg.Items.SortDescriptions) {
            sortInfos.Add(sortDescription);
        }
        return sortInfos;
    }
    
    void SetColumnInfo(DataGrid dg, List<DataGridColumn> columnInfos) {
        columnInfos.Sort((c1, c2) => { return c1.DisplayIndex - c2.DisplayIndex; });
        foreach (var columnInfo in columnInfos) {
            var column = dg.Columns.FirstOrDefault(col => col.Header == columnInfo.Header);
            if (column != null) {
                column.SortDirection = columnInfo.SortDirection;
                column.DisplayIndex = columnInfo.DisplayIndex;
                column.Visibility = columnInfo.Visibility;
            }
        }
    }
    
    void SetSortInfo(DataGrid dg, List<SortDescription> sortInfos) {
        dg.Items.SortDescriptions.Clear();
        foreach (var sortInfo in sortInfos) {
            dg.Items.SortDescriptions.Add(sortInfo);
        }
    }
    

    【讨论】:

    • 我知道这是古老的,但只是想指出,在这个例子中,DataGridColumns 是通过引用存储的,这意味着通过刷新 DataSource 所做的任何更改也会更新“存储' 数据网格列。相反,应该克隆来自 DataGridColumn 的数据。
    【解决方案2】:

    您是否尝试过获取数据集的集合视图?

    CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions
    

    这将为您提供当前排序描述的数组。然后你可以坚持这些,下一轮应用它们如下

    CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions.Add(...)
    

    希望对你有帮助。

    【讨论】:

    • 是的!这应该是正确的解决方案..也是唯一的解决方案。如果我是正确的,你甚至不必“记住”之前的排序或分组:一旦它们被声明为你的 collectionViewSource,所有新添加的项目都会相应地排序,就好像你只是做了一个蛮力'.Refresh ()' 上的 'ICollectionView' 对象...
    【解决方案3】:

    我的一位同事想出了这个。它似乎工作正常。唯一的问题是我认为 DataGrid 中的列标题需要与 DB 中的相同。

    string sortHeader;
    string prevSortHeader;
    SortDescription sd;
    
    private void dgInvoiceHeads_Sorting(object sender, DataGridSortingEventArgs e) {
      sortHeader = e.Column.Header.ToString();
    
      if (sortHeader == prevSortHeader) {
        sd = new SortDescription(sortHeader, ListSortDirection.Descending);
      }
      else {
        sd = new SortDescription(sortHeader, ListSortDirection.Ascending);
      }
      prevSortHeader = sortHeader;
    }
    

    HTH

    【讨论】:

    • 您可以通过在列实例中使用 SortMemberPath 而不是 Header 属性来解决标题必须与类成员相同的问题。 sortHeader = e.Column.SortMemberPath
    • sd 未在上述代码示例中使用。那么它是如何工作的呢?
    【解决方案4】:
     private void testGrid_Sorting(object sender, DataGridSortingEventArgs e)
            {
    
     ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ?
                                    ListSortDirection.Ascending : ListSortDirection.Descending;
    
    // You will get the current direction in direction
    
            }
    
    This is another solution
    

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2013-06-16
      • 2013-11-24
      • 2020-05-15
      • 2012-05-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多