/// <summary> /// Event fired when a user clicks a column button (Sort Button) in the listUsers control. /// This method toggles the sort direction for each column in each list mode. /// </summary> private void listUsers_ColumnClick(object sender, ColumnClickEventArgs e) { ULColumnType selColumn = (ULColumnType)listUsers.Columns[e.Column].Tag; if (CurrentSortDirection[CurrentListSource] == ULSortDirection.Ascending) { CurrentSortDirection[CurrentListSource] = ULSortDirection.Descending; } else if (CurrentSortDirection[CurrentListSource] == ULSortDirection.Descending) { CurrentSortDirection[CurrentListSource] = ULSortDirection.Ascending; } if (CurrentSortColumn[CurrentListSource] != selColumn) { CurrentSortDirection[CurrentListSource] = ULSortDirection.Descending; } CurrentSortColumn[CurrentListSource] = selColumn; listUsers.SetRows(CurrentUserRows, CurrentSortColumn[CurrentListSource], CurrentSortDirection[CurrentListSource]); }
public static void SetRows(this ListView Owner, UserRows Rows, ULColumnType SortColumn = ULColumnType.Tag, ULSortDirection SortDirection = ULSortDirection.Ascending) { Owner.BeginUpdate(); Owner.SuspendLayout(); Owner.Items.Clear(); foreach (ColumnHeader colHead in Owner.Columns) { ULColumnType colType = (ULColumnType)colHead.Tag; string arrow = ""; if (colType == SortColumn) { if (SortDirection == ULSortDirection.Ascending) { arrow = " ↑"; } if (SortDirection == ULSortDirection.Descending) { arrow = " ↓"; } } colHead.Text = CurrentHeaderRow[colType] + arrow; } if (Rows != null) { Rows.Sort(delegate(UserRow x, UserRow y) { if (!x.ContainsKey(SortColumn) && !y.ContainsKey(SortColumn)) { return(0); } if (!x.ContainsKey(SortColumn) || x[SortColumn] == null) { return(-1); } if (!y.ContainsKey(SortColumn) || y[SortColumn] == null) { return(1); } if (x[SortColumn] == y[SortColumn]) { return(0); } long intX, intY = 0; if (long.TryParse(x[SortColumn], out intX) && long.TryParse(y[SortColumn], out intY)) { if (intX > intY) { return(-1); } if (intX < intY) { return(1); } } return(x[SortColumn].CompareTo(y[SortColumn])); }); if (SortDirection == ULSortDirection.Ascending) { Rows.Reverse(); } foreach (UserRow row in Rows) { ListViewItem lvRow = null; bool first = true; foreach (KeyValuePair <ULColumnType, string> column in row) { if (column.Key == ULColumnType.Tag) { continue; } string value = ConvertColumnValue(column); if (first) { lvRow = Owner.Items.Add(value); lvRow.ImageKey = "user"; lvRow.Tag = row[ULColumnType.Tag]; first = false; } else { lvRow.SubItems.Add(value); } } } } Owner.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); Owner.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); Owner.ResumeLayout(); Owner.EndUpdate(); }