0

我的 DataGrid 又出现问题了……这次:如何在编辑单元格时关闭排序?

例如:

在此处输入图像描述

我最后添加了标记的“A”,它跳到顶部,因为该列已排序。但它应该留在按钮上。如果您对设置文件(在 Visual Studio 中)进行排序,它的工作方式与我想要的完全一样。你可以自己尝试一下,这里是VS中的同一个例子: 在此处输入图像描述

我试图重置SortDirection,不起作用:

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

2 回答 2

0

Helper.SetGridViewSortState(this.dgv, DataGridViewColumnSortMode.NotSortable); 请试试这个

于 2014-11-14T17:16:55.523 回答
0

找到了解决方案:

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

CollectionChanged并为来自的事件添加一个事件处理程序ObservableCollection<T>

 void VATINS_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
 {
    ResetSorting();
 }
于 2014-11-14T17:52:12.410 回答