【问题标题】:Turn DataGridColumn sorting off关闭 DataGridColumn 排序
【发布时间】:2014-11-14 16:28:00
【问题描述】:

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

例如:

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

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

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

【问题讨论】:

  • 这适用于 DataGridView(Windows 窗体)而不是 WPF 中的 DataGrid
  • @reggaeguitar 他的意思是你建议的链接是关于 Winforms,但他正在使用 WPF DataGrid。

标签: c# wpf sorting datagrid sortdirection


【解决方案1】:

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

【讨论】:

  • 它用于 DataGirdView,我正在寻找 DataGrid(在 WPF 中)的解决方案
【解决方案2】:

找到解决办法:

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

并从ObservableCollection&lt;T&gt;CollectionChanged 事件添加事件处理程序

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

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 2011-12-17
    • 2020-03-02
    • 2010-11-22
    • 2010-10-03
    • 2015-04-14
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多