void HandleSort(string sortExpression, SortDirection sortDirection) { DataGridViewSortEventArgs e = new DataGridViewSortEventArgs(sortExpression, sortDirection); this.OnSorting(e); if (!e.Cancel) { string strHeaderText = string.Empty; foreach (DataControlFieldEx field in this.Columns) { strHeaderText = field.HeaderText; if (!string.IsNullOrEmpty(strHeaderText)) { //清除所有的排序方向标志↑↓ strHeaderText = strHeaderText.Replace("↑", "").Replace("↓", ""); //给当前排序列加方向标志 if (e.SortExpression == field.SortExpression) { strHeaderText += (e.SortDirection == SortDirection.Ascending) ? "↑" : "↓"; } //重新设置排序方向标志 field.HeaderText = strHeaderText; } } this.SortExpression = e.SortExpression; this.SortDirection = e.SortDirection; this.PageIndex = 0; this.OnSorted(EventArgs.Empty); this.RequiresDataBinding = true; } }
/// <summary> /// 触发<see cref="Sorting"/>事件。 /// </summary> /// <param name="e"></param> protected virtual void OnSorting(DataGridViewSortEventArgs e) { DataGridViewSortEventHandler handler = this.Sorting; if (handler != null) { handler(this, e); } }