C# ListView点击列头进行排序
1 /// <summary> 2 /// This class is an implementation of the 'IComparer' interface. 3 /// </summary> 4 public class ListViewColumnSorter : IComparer 5 { 6 /// <summary> 7 /// Specifies the column to be sorted 8 /// </summary> 9 private int ColumnToSort; 10 /// <summary> 11 /// Specifies the order in which to sort (i.e. 'Ascending'). 12 /// </summary> 13 private SortOrder OrderOfSort; 14 /// <summary> 15 /// Case insensitive comparer object 16 /// </summary> 17 private CaseInsensitiveComparer ObjectCompare; 18 19 /// <summary> 20 /// Class constructor. Initializes various elements 21 /// </summary> 22 public ListViewColumnSorter() 23 { 24 // Initialize the column to '0' 25 ColumnToSort = 0; 26 27 // Initialize the sort order to 'none' 28 OrderOfSort = SortOrder.None; 29 30 // Initialize the CaseInsensitiveComparer object 31 ObjectCompare = new CaseInsensitiveComparer(); 32 } 33 34 private int comaretInt = 0; 35 /// <summary> 36 /// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison. 37 /// </summary> 38 /// <param name="x">First object to be compared</param> 39 /// <param name="y">Second object to be compared</param> 40 /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns> 41 public int Compare(object x, object y) 42 { 43 int compareResult; 44 ListViewItem listviewX, listviewY; 45 46 // Cast the objects to be compared to ListViewItem objects 47 listviewX = (ListViewItem)x; 48 listviewY = (ListViewItem)y; 49 50 if (int.TryParse(listviewX.SubItems[ColumnToSort].Text, out comaretInt) && 51 int.TryParse(listviewY.SubItems[ColumnToSort].Text, out comaretInt)) 52 { 53 compareResult = int.Parse(listviewX.SubItems[ColumnToSort].Text).CompareTo(int.Parse(listviewY.SubItems[ColumnToSort].Text)); 54 } 55 else 56 { 57 // Compare the two items 58 compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); 59 } 60 // Calculate correct return value based on object comparison 61 if (OrderOfSort == SortOrder.Ascending) 62 { 63 // Ascending sort is selected, return normal result of compare operation 64 return compareResult; 65 } 66 else if (OrderOfSort == SortOrder.Descending) 67 { 68 // Descending sort is selected, return negative result of compare operation 69 return (-compareResult); 70 } 71 else 72 { 73 // Return '0' to indicate they are equal 74 return 0; 75 } 76 } 77 78 /// <summary> 79 /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0'). 80 /// </summary> 81 public int SortColumn 82 { 83 set 84 { 85 ColumnToSort = value; 86 } 87 get 88 { 89 return ColumnToSort; 90 } 91 } 92 93 /// <summary> 94 /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending'). 95 /// </summary> 96 public SortOrder Order 97 { 98 set 99 { 100 OrderOfSort = value; 101 } 102 get 103 { 104 return OrderOfSort; 105 } 106 } 107 108 }
将上面的类添加到你的项目里面。
1 private ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter(); 2 3 4 void listTask_ColumnClick(object sender, ColumnClickEventArgs e) 5 { 6 foreach (ColumnHeader ch in listTask.Columns) 7 { 8 ch.ImageIndex = -1; 9 } 10 11 listTask.ListViewItemSorter = lvwColumnSorter; 12 13 //if (listTask.SmallImageList == null) 14 //{ 15 // listTask.SmallImageList = imageList1; 16 //} 17 18 //listTask.Columns[e.Column].ImageIndex = 0; 19 20 if (e.Column == lvwColumnSorter.SortColumn) 21 { 22 // Reverse the current sort direction for this column. 23 if (lvwColumnSorter.Order == SortOrder.Ascending) 24 { 25 lvwColumnSorter.Order = SortOrder.Descending; 26 //listTask.Columns[e.Column].ImageIndex = 1; 27 } 28 else 29 { 30 lvwColumnSorter.Order = SortOrder.Ascending; 31 } 32 } 33 else 34 { 35 // Set the column number that is to be sorted; default to ascending. 36 lvwColumnSorter.SortColumn = e.Column; 37 lvwColumnSorter.Order = SortOrder.Ascending; 38 } 39 40 // Perform the sort with these new sort options. 41 42 this.listTask.Sort(); 43 listTask.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); //根据列的文本内容,自动设置list列的宽度 44 listTask.Update(); 45 46 }