I have a WPF DataGrid that is populated with data from DataSet. I have CanUserSortColumns
set to true.
我有一个WPF DataGrid,其中填充了DataSet中的数据。我将CanUserSortColumns设置为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.
但我似乎无法保持指定的排序。
4 个解决方案
#1
3
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);
}
}
#2
3
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.
希望能帮助到你。
#3
3
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.
我的一位同事提出了这个问题。它似乎工作正常。唯一的问题是我认为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
HTH
#4
1
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
#1
3
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);
}
}
#2
3
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.
希望能帮助到你。
#3
3
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.
我的一位同事提出了这个问题。它似乎工作正常。唯一的问题是我认为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
HTH
#4
1
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