Cast the objects to be compared to ListViewItem objects "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y' It compares the two objects passed using a case insensitive comparison. / This method is inherited from the IComparer interface. ObjectCompare = new CaseInsensitiveComparer() Initialize the CaseInsensitiveComparer object Private CaseInsensitiveComparer ObjectCompare / Specifies the order in which to sort (i.e. Public class ListViewColumnSorter : IComparer / This class is an implementation of the 'IComparer' interface. LvwColumnSorter = new ListViewColumnSorter() Create an instance of a ListView column sorter and assign it to the ListView control. Private ListViewColumnSorter lvwColumnSorter Made minor changes to the article here to accommodate sorting of both string and numeric values in ListView. If (SortDirection = SortOrder.Descending) Long yLong = DataParseUtility.ParseLong() Long xLong = DataParseUtility.ParseLong() Short yShort = DataParseUtility.ParseShort() Short xShort = DataParseUtility.ParseShort() Sorter.SortDirection = SortOrder.Ascending īelow is my ListViewItemComparer public class ListViewItemComparer : IComparerĭateTime xDt = DataParseUtility.ParseDate() ĭateTime yDt = DataParseUtility.ParseDate() ĭecimal xD = DataParseUtility.ParseDecimal(("$", string.Empty).Replace(",", string.Empty)) ĭecimal yD = DataParseUtility.ParseDecimal(("$", string.Empty).Replace(",", string.Empty)) Sorter.SortDirection = SortOrder.Descending If (sorter.SortDirection = SortOrder.Ascending) Sorter.ColumnType = ColumnDataType.String Sorter.ColumnType = ColumnDataType.Decimal Sorter.ColumnType = ColumnDataType.DateTime Ĭase ApplicationModel.Displa圜olumns.NetTotal:Ĭase ApplicationModel.Displa圜olumns.GrossTotal: String columnName = Ĭase ApplicationModel.Displa圜olumns.DateCreated:Ĭase ApplicationModel.Displa圜olumns.DateUpdated: ListViewItemComparer sorter = (ListViewItemComparer)listView.ListViewItemSorter Private ListViewItemComparer GetListViewSorter(int columnIndex) ListViewItemComparer sorter = GetListViewSorter(e.Column) private void listView_ColumnClick(object sender, ColumnClickEventArgs e) Here's a snippet from my ColumnClick event handler. I sort using column name to set any sorting specifics that may need to be handled based on data type stored in the column and or if the column has already been sorted on(asc/desc).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |