简体   繁体   中英

Turn DataGridColumn sorting off

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:

在此处输入图片说明

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: 在此处输入图片说明

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM