Reputation: 792
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.
Upvotes: 9
Views: 11401
Reputation: 11
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
Upvotes: 1
Reputation: 792
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
Upvotes: 3
Reputation: 4076
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.
Upvotes: 5
Reputation: 41403
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);
}
}
Upvotes: 4