Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 431
Noelle Avatar asked Jun 24 '11 13:06

Noelle


People also ask

How to group sort and filter data in the DataGrid control?

To group, sort, and filter the data in a DataGrid, you bind it to a CollectionViewSource. You can then manipulate the data in the backing data source using LINQ queries without affecting the underlying data. The changes in the collection view are reflected in the DataGrid user interface (UI).

How do I sort a column in DataGrid WPF?

WPF DataGrid (SfDataGrid) allows you to sort the data against one or more columns by clicking a column header by pressing <kbd>Ctrl</kbd> key. Sorting orders to denote the order of sorting performed for columns can be enabled by using the DataGrid.

Can user reorder columns WPF?

Can user reorder columns WPF? RadGridView supports column reordering and it can be done by the user in run-time. The user can drag the desired column's header at the desired position among the other headers and drop it there.

How to filter data in a DataGrid WPF?

To filter items in a DataGridAdd a handler for the CollectionViewSource. Filter event. In the Filter event handler, define the filtering logic. The filter will be applied every time the view is refreshed.


2 Answers

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);
    }
}
like image 92
CodeNaked Avatar answered Sep 19 '22 11:09

CodeNaked


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.

like image 38
Stefan Z Camilleri Avatar answered Sep 20 '22 11:09

Stefan Z Camilleri