[英]Turn DataGridColumn sorting off
我的DataGrid再次出現問題...這次:編輯單元格時如何關閉排序?
例如:
我在最后加上標記的“ A”,由於列已排序,因此它跳到了頂部。 但它應該留在按鈕上。 如果對設置文件進行排序(在Visual Studio中),則其工作原理與我想要的完全相同。 您可以自己嘗試,這是VS中的相同示例:
我試圖重置SortDirection
,但無法正常工作:
private void dgVATINS_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
foreach (DataGridColumn col in dgVATINS.Columns)
{
col.SortDirection = null;
}
}
Helper.SetGridViewSortState(this.dgv,DataGridViewColumnSortMode.NotSortable); 請嘗試這個
找到一個解決方案:
/// <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>
將事件處理程序添加到CollectionChanged
事件
void VATINS_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
ResetSorting();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.