打开DataGridColumn排序关闭

问题描述:

我的DataGrid存在问题,再次...此时:如何在编辑单元格时关闭排序?打开DataGridColumn排序关闭

例如:

enter image description here

我添加了标有“A”最后也是因为列进行排序跃升到顶部。但它应该停留在按钮上。如果您对设置文件进行排序(在Visual Studio中),它的工作原理与我想要的完全相同。你可以自己尝试一下,这里是在VS同样的例子: enter image description here

我试图重置SortDirection,不工作:

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

这是在DataGridView(Windows窗体)添加一个事件处理程序CollectionChanged事件不为DataGrid在WPF – MrApfelstrudel 2014-11-14 16:58:19

+0

@reggaeguitar他意味着你建议的关于Winforms的链接,但他正在使用WPF DataGrid。 – 2014-11-14 17:12:10

+0

这可能吗? http://*.com/questions/13401869/wpf-datagrid-clear-column-sorting – reggaeguitar 2014-11-14 17:14:08

找到了解决办法:

/// <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<T>

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

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

+0

它的DataGirdView,我正在寻找一个DataGrid的解决方案(在WPF中) – MrApfelstrudel 2014-11-14 17:23:59