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.
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).
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? 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With