在 WPF DataGrid 中保留用户定义的排序顺序

发布于 2024-11-17 04:03:30 字数 357 浏览 2 评论 0原文

我有一个 WPF DataGrid,其中填充了 DataSet 中的数据。我将 CanUserSortColumns 设置为 true

网格刷新时是否可以保留用户指定的排序? 选择的项目

  object selectedItem = dgInvoiceHeads.SelectedItem;

我让它保留在刷新发生之前

 dgInvoiceHeads.SelectedItem = selectedItem;

,然后在刷新发生之后放置。

但我似乎无法让它保留指定的排序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放手` 2024-11-24 04:03:30

您是否尝试过获取数据集的集合视图?

CollectionViewSource.GetDefaultView(yourDataSet).SortDescriptions

这将为您提供当前排序描述的数组。然后,您可以坚持这些,并在下一次应用它们,如下

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

希望它有帮助。

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.

香草可樂 2024-11-24 04:03:30

以下代码是从此 论坛帖子,它展示了如何获取排序描述和列信息并恢复它。

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

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);
    }
}
萌︼了一个春 2024-11-24 04:03:30

我的一位同事想出了这个办法。它似乎工作正常。唯一的问题是我认为 DataGrid 中的列标题需要与数据库中的列标题相同。

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

华泰

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

小兔几 2024-11-24 04:03:30
 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
 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文