Changeset 14 for Common/ListViewComparer.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/ListViewComparer.cs
r1 r14 2 2 using System.Windows.Forms; 3 3 using System.Runtime.InteropServices; 4 using System. Globalization;4 using System.Collections; 5 5 6 6 namespace Common 7 7 { 8 public enum ColumnDataType { String, Integer, DateTime};8 public enum ColumnDataType { String, Integer, DateTime, Decimal, Custom }; 9 9 10 10 // Supports sorting by column in ascending or descending order 11 public class ListViewItemComparer : System.Collections.IComparer11 public class ListViewItemComparer : IComparer 12 12 { 13 private int column = 0;14 private SortOrder order = 0;15 16 public void ColumnClick(int column)13 private int column; 14 private SortOrder order; 15 16 public void ColumnClick(int newColumn) 17 17 { 18 18 // Determine if clicked column is already the column that is being sorted. 19 if ( column == this.column)19 if (newColumn == column) 20 20 { 21 21 // Reverse the current sort direction for this column. … … 32 32 { 33 33 // Set the column number that is to be sorted; default to ascending. 34 this.column = column;34 column = newColumn; 35 35 order = SortOrder.Ascending; 36 36 } 37 37 } 38 38 39 public void SetColumn(int column)40 { 41 this.column = column;42 } 43 44 public void SetOrder(SortOrder order)45 { 46 this.order = order;39 public void SetColumn(int newColumn) 40 { 41 column = newColumn; 42 } 43 44 public void SetOrder(SortOrder newOrder) 45 { 46 order = newOrder; 47 47 } 48 48 … … 53 53 } 54 54 55 public delegate int CompareItemEventHandler(ListViewItem x, ListViewItem y, int column); 56 57 public event CompareItemEventHandler OnCompareItem; 58 55 59 public int Compare(object x, object y) 56 60 { 61 ListViewItem listViewItemX = (ListViewItem)x; 62 ListViewItem listViewItemY = (ListViewItem)y; 63 57 64 int result = 0; 58 if ((column < ((ListViewItem)x).SubItems.Count) && (column < ((ListViewItem)y).SubItems.Count) && (column >= 0)) 59 { 60 ColumnDataType dataType = ColumnDataType.String; 61 62 if (((ListViewItem)x).ListView.Columns[column].Tag != null) 63 dataType = (ColumnDataType)(((ListViewItem)x).ListView.Columns[column].Tag); 64 if (dataType == ColumnDataType.Integer) 65 { 66 int xi = 0; 67 int yi = 0; 68 if (int.TryParse(((ListViewItem)x).SubItems[column].Text, out xi) && 69 int.TryParse(((ListViewItem)y).SubItems[column].Text, out yi)) 70 { 71 if (xi < yi) result = -1; 72 else if (xi > yi) result = 1; 73 else result = 0; 74 } 75 } else 76 if (dataType == ColumnDataType.DateTime) 77 { 78 DateTime xi; 79 DateTime yi; 80 if (DateTime.TryParse(((ListViewItem)x).SubItems[column].Text, out xi) && 81 DateTime.TryParse(((ListViewItem)y).SubItems[column].Text, out yi)) 82 { 83 result = DateTime.Compare(xi, yi); 84 } 85 } 86 else result = String.Compare(((ListViewItem)x).SubItems[column].Text, ((ListViewItem)y).SubItems[column].Text); 87 } 88 if (order == SortOrder.Ascending) 89 { 90 return result; 91 } 92 else if (order == SortOrder.Descending) 93 { 94 return -result; 95 } 96 else 97 { 98 return 0; 65 if (listViewItemX != null && listViewItemY != null) 66 { 67 if (column < listViewItemX.SubItems.Count && column < listViewItemY.SubItems.Count && column >= 0) 68 { 69 ColumnDataType dataType = ColumnDataType.String; 70 71 if (listViewItemX.ListView.Columns[column].Tag != null) 72 { 73 dataType = (ColumnDataType) (listViewItemX.ListView.Columns[column].Tag); 74 } 75 76 if (dataType == ColumnDataType.Integer) 77 { 78 if (int.TryParse(listViewItemX.SubItems[column].Text, out var xi) && 79 int.TryParse(listViewItemY.SubItems[column].Text, out var yi)) 80 { 81 if (xi < yi) result = -1; 82 else if (xi > yi) result = 1; 83 else result = 0; 84 } 85 } 86 else if (dataType == ColumnDataType.Decimal) 87 { 88 if (decimal.TryParse(listViewItemX.SubItems[column].Text, out var xi) && 89 decimal.TryParse(listViewItemY.SubItems[column].Text, out var yi)) 90 { 91 if (xi < yi) result = -1; 92 else if (xi > yi) result = 1; 93 else result = 0; 94 } 95 } 96 else if (dataType == ColumnDataType.DateTime) 97 { 98 if (DateTime.TryParse(listViewItemX.SubItems[column].Text, out var xi) && 99 DateTime.TryParse(listViewItemY.SubItems[column].Text, out var yi)) 100 { 101 result = DateTime.Compare(xi, yi); 102 } 103 } 104 else if (dataType == ColumnDataType.Custom) 105 { 106 if (OnCompareItem != null) 107 { 108 result = OnCompareItem(listViewItemX, listViewItemY, column); 109 } 110 } 111 else 112 result = string.CompareOrdinal(listViewItemX.SubItems[column].Text, 113 listViewItemY.SubItems[column].Text); 114 } 115 else 116 { 117 if (!listViewItemX.Selected && listViewItemY.Selected) result = 1; 118 else if (listViewItemX.Selected && !listViewItemY.Selected) result = -1; 119 } 120 } 121 122 switch (order) 123 { 124 case SortOrder.Ascending: 125 return result; 126 case SortOrder.Descending: 127 return -result; 128 default: 129 return 0; 99 130 } 100 131 } … … 103 134 { 104 135 if (listView.ListViewItemSorter == null) 136 { 105 137 listView.ListViewItemSorter = new ListViewItemComparer(); 106 (listView.ListViewItemSorter as ListViewItemComparer).SetOrder(order); 107 (listView.ListViewItemSorter as ListViewItemComparer).SetColumn(column); 138 } 139 140 ((ListViewItemComparer) listView.ListViewItemSorter).SetOrder(order); 141 ((ListViewItemComparer) listView.ListViewItemSorter).SetColumn(column); 108 142 if ((column != -1) && (order != SortOrder.None)) 109 143 { … … 113 147 listView.ColumnClick += delegate (object sender, ColumnClickEventArgs e) 114 148 { 115 (( sender as ListView).ListViewItemSorter as ListViewItemComparer).ColumnClick(e.Column);116 ( sender as ListView).Sort();117 ( sender as ListView).SetSortIcon(((sender as ListView).ListViewItemSorter as ListViewItemComparer).column, ((sender as ListView).ListViewItemSorter as ListViewItemComparer).order);149 ((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).ColumnClick(e.Column); 150 ((ListView) sender).Sort(); 151 ((ListView) sender).SetSortIcon(((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).column, ((ListViewItemComparer) ((ListView) sender).ListViewItemSorter).order); 118 152 }; 119 153 } … … 181 215 182 216 183 // This method used to set arrow icon217 // This method used to set arrow icon 184 218 public static void SetSortIcon(this ListView listView, int columnIndex, SortOrder order) 185 219 { … … 189 223 { 190 224 IntPtr columnPtr = new IntPtr(columnNumber); 191 LVCOLUMN lvColumn = new LVCOLUMN(); 192 lvColumn.mask = HDI_FORMAT; 225 LVCOLUMN lvColumn = new LVCOLUMN {mask = HDI_FORMAT}; 193 226 194 227 SendMessageLVCOLUMN(columnHeader, HDM_GETITEM, columnPtr, ref lvColumn); 195 228 196 if ( !(order == SortOrder.None)&& columnNumber == columnIndex)229 if (order != SortOrder.None && columnNumber == columnIndex) 197 230 { 198 231 switch (order) 199 232 { 200 case S ystem.Windows.Forms.SortOrder.Ascending:233 case SortOrder.Ascending: 201 234 lvColumn.fmt &= ~HDF_SORTDOWN; 202 235 lvColumn.fmt |= HDF_SORTUP; 203 236 break; 204 case S ystem.Windows.Forms.SortOrder.Descending:237 case SortOrder.Descending: 205 238 lvColumn.fmt &= ~HDF_SORTUP; 206 239 lvColumn.fmt |= HDF_SORTDOWN;
Note:
See TracChangeset
for help on using the changeset viewer.