开发者

Retain the user defined sort order in WPF DataGrid

I have a WPF DataGrid that is populated with data from DataSet. I have CanUserSortColumns set to true.

Is it possible to retain the sorting that the user specified when the grid is refreshed? I have it retaining the item that was selected using

  object selectedItem = dgInvoiceHeads.SelectedItem;

before the refresh takes place开发者_开发知识库 and then placing

 dgInvoiceHeads.SelectedItem = selectedItem;

after the refresh takes place.

But I can't seem to get it to retain the specified sort.


The following code was pulled from this forum post and it shows how to obtain the sort descriptions and column information and restore it.

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


Have you tried getting the collectionview for the dataset?

CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions

This will give you an array of the current sortdescriptions. You can then persist these, and the next time round apply them as follows

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

Hope it helps.


One of my colleagues came up with this. It seems to be working correctly. The only thing is I think the column headers need to be the same in the DataGrid as they are in the 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


 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜