我在 C# 程序中使用 ObjectListView 对象,我想知道是否可以更改默认的排序行为。
开箱即用,第一次单击每列时,控件会按升序对列进行排序。但我希望第一次对每列进行降序排序。 这可能吗? 我的应用程序的用户不希望必须单击每一列两次才能到达该列的降序排序。
此列点击排序行为是硬编码的。也许您可以解决这个问题,方法是在启动时将排序顺序设置为“升序”(每列一个),因此第一次单击将导致降序。
/// <summary>
/// Event handler for the column click event
/// </summary>
protected virtual void HandleColumnClick(object sender, ColumnClickEventArgs e) {
if (!this.PossibleFinishCellEditing())
return;
// Toggle the sorting direction on successive clicks on the same column
if (this.PrimarySortColumn != null && e.Column == this.PrimarySortColumn.Index)
this.PrimarySortOrder = (this.PrimarySortOrder == SortOrder.Descending ? SortOrder.Ascending : SortOrder.Descending);
else
this.PrimarySortOrder = SortOrder.Ascending;
this.BeginUpdate();
try {
this.Sort(e.Column);
} finally {
this.EndUpdate();
}
}