WPF 数据网格:清除列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13401869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:14:12  来源:igfitidea点击:

WPF Datagrid: Clear column sorting

c#wpfwpfdatagrid

提问by Hussein Khalil

I am using a WPF Datagrid in my application where columns can be sorted by clicking on the header.

我在我的应用程序中使用 WPF Datagrid,其中可以通过单击标题对列进行排序。

I was wondering if there was any way to clear a column's sorting programatically ?

我想知道是否有任何方法可以以编程方式清除列的排序?

I tried sorting a column and then clearing MyDataGrid.Items.SortDescriptions, but that collection was empty (even though one column was sorted).

我尝试对一列进行排序然后清除MyDataGrid.Items.SortDescriptions,但该集合是空的(即使对一列进行了排序)。

I also tried :

我也试过:

MyDataGridColumn.SortDirection = null;

The problem is that the column indication is gone, but the sorting still occurs when editing a cell and switching rows.

问题是列指示消失了,但是在编辑单元格和切换行时仍然会发生排序。

Is there no way to clear a column's sort ?

有没有办法清除列的排序?

Edit (for clarity): The problem is that I'd like to allow sorting again if the user re-clicks on the same column header, so setting CanUserSort to false would be problematic, even if it were done in the XAML. In short, what I'm attempting to do, is prevent rows from being ordered once a sorted column has a cell that was modified. I want to force the user to re-click on the header.

编辑(为清楚起见):问题是,如果用户重新单击同一列标题,我希望再次允许排序,因此将 CanUserSort 设置为 false 会出现问题,即使它是在 XAML 中完成的。简而言之,我正在尝试做的是,一旦排序的列有一个被修改的单元格,就阻止对行进行排序。我想强制用户重新单击标题。

回答by NoOne

Here is what you need:

这是您需要的:

using System.Windows.Data;
using System.ComponentModel;

ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
if (view != null)
{
    view.SortDescriptions.Clear();
    foreach (DataGridColumn column in grid.Columns)
    {
        column.SortDirection = null;
    }
}

Original source: https://stackoverflow.com/a/9533076/964053

原始来源:https: //stackoverflow.com/a/9533076/964053

What I want to know is what was M$ thinking for not putting a ClearSort() method...

我想知道的是 M$ 是怎么想的不把 ClearSort() 方法...

回答by Rohit Vats

Set CanUserSortto falsefor all columns -

设置CanUserSortfalse所有列-

foreach (var a in MyDataGrid.Columns)
{
    a.CanUserSort = false;
}

回答by Harry

as an extension...

作为扩展...

    public static  void ClearSort(this DataGrid grid)
    {
        var view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
        view?.SortDescriptions.Clear();

        foreach (var column in grid.Columns)
        {
            column.SortDirection = null;
        }
    }

回答by Jassim

In XAML you can turn it off using this code.

在 XAML 中,您可以使用此代码将其关闭。

<DataGridTextColumn Header="Header Name" CanUserSort="False"/>

回答by iato

This is what I use in my program (I have a RESET button and use this to clear the sorting on a datagrid).

这就是我在我的程序中使用的(我有一个 RESET 按钮并使用它来清除数据网格上的排序)。

System.Windows.Data.CollectionViewSource.GetDefaultView(MY_DATA_GRID.ItemsSource).SortDescriptions.Clear();

Works like a charm.

奇迹般有效。

Cheers, Iato

干杯,伊托

回答by Vaibhav

In the XAML code of DataGrid you can add CanUserSortColumns="False". Then noboady would be able to sort any column at rumtime.

在 DataGrid 的 XAML 代码中,您可以添加 CanUserSortColumns="False"。然后没有人能够在 rumtime 对任何列进行排序。

回答by Vaibhav

This is a small code snippet to disable the sorting of DataGridView.

这是一个小代码片段,用于禁用DataGridView.

for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
    dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}