Turn DataGridColumn sorting off

68 views Asked by At

I have a problem with my DataGrid, again... This time: How can I turn off the sorting when I am editing a cell?

For example:

enter image description here

I add the marked 'A' last and it jumped to the top because the column is sorted. But it should stay at the button. If you sort a settings file (in Visual Studio) it works exactly like I want to. You can try it yourself, here is the same example in VS: enter image description here

I tried to reset the SortDirection, doesn't work:

    private void dgVATINS_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        foreach (DataGridColumn col in dgVATINS.Columns)
        {
            col.SortDirection = null;
        }
    }
2

There are 2 answers

0
MrApfelstrudel On BEST ANSWER

Found a solution:

 /// <summary>
    /// Resets the sorting.
    /// </summary>
    public void ResetSorting()
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(dgVATINS.ItemsSource); // Gets the default view of the DataGrid
        if (view != null && view.SortDescriptions.Count > 0)
        {
            view.SortDescriptions.Clear(); // Clears the sorting

            foreach (DataGridColumn column in dgVATINS.Columns)
            {
                column.SortDirection = null;
            };
        }
    }

And add an event handler to the CollectionChanged event from the ObservableCollection<T>

 void VATINS_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
 {
    ResetSorting();
 }
1
renu On

Helper.SetGridViewSortState(this.dgv, DataGridViewColumnSortMode.NotSortable); Please try this