WPF Datagrid: Clear column sorting
Solution 1
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
What I want to know is what was M$ thinking for not putting a ClearSort() method...
Solution 2
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;
}
}
Solution 3
Set CanUserSort
to false
for all columns -
foreach (var a in MyDataGrid.Columns)
{
a.CanUserSort = false;
}
Related videos on Youtube
Hussein Khalil
Software Engineer currently working in the Video Games industry.
Updated on September 16, 2022Comments
-
Hussein Khalil over 1 year
I am using a WPF Datagrid in my application where columns can be sorted by clicking on the header.
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).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.
-
Hussein Khalil over 11 yearsWon't this prevent columns from being sorted again ? I'm just looking to remove sorting for a particular column, not disable it for all columns.
-
Rohit Vats over 11 yearsSo, isn't that what you want?
-
Rohit Vats over 11 yearsCan't you set for those particular columns in xaml this property to false?
-
ean5533 over 11 years@RV1987 I think he wants the user to be able to sort all columns manually, but he also wants to programmatically change the sorting in response to user input (say, a button press)
-
Hussein Khalil over 11 yearsThe 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. @ean5533 Correct :)
-
Ren about 11 yearsIt has already been mentioned in the question that this technique is not suitable.
-
Vaibhav about 11 years@Ren - This is a small code snipet to disable the sorting of DataGridView. for (int i = 0; i < dataGridView1.ColumnCount; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; }
-
Ren about 11 yearsIf you read the question, you will see that this is not what the author wants. He wants to prevent column sorting automatically when users make edits to cells but he wants to leave sorting enabled so that the users can still click a column heading to sort the table if they so wish.
-
Alexandru Dicu about 5 yearsThe question was how to clear it once it is sorted, not how to remove the sort entirely.
-
JMIII over 3 yearsYeah wondering the same thing. I ultimately used an attached behavior when the ItemSourceProperty changed to clear.