How to sort the items of ListView

zhaozj2021-02-16  47

How to sort the items of ListView

When you use a resource browser to view a file, you can sort with the names, size, type, and modification date you want to do with the names. The ListView component provided by .NET does not provide this function directly, but it is not difficult to implement. The functionality of the listview.sort () method is "Sort the items of the list view", but you have not happened when you call it, because you don't specify a nicknator for the listview1.listviewItemsORTER. Therefore, first, you must write a sort class. public class mySorter: IComparer {private Comparer comparer; private int sortColumn; private SortOrder sortOrder; public mySorter () {sortColumn = 0; sortOrder = SortOrder.None; comparer = Comparer.Default;} // sort column specified public int SortColumn {GET {Return SortColumn;} set {sortcolumn = value;}} // Specify PUBLIC Sortorder Sortorder {Get {Return Sortorder;} set {sortorder = value;}} publici y) {int CompareResult; ListViewItem itemX = (ListViewItem) x; ListViewItem itemY = (ListViewItem) y; // where you can provide a custom ordering CompareResult = comparer.Compare (itemX.SubItems [this.sortColumn] .Text, Itemy.Subitems [this.SortColumn] .text); if (this.Sortorder == Sortorder.ascending) Return Compareresult; Elseif (this.Sortorder == Sortorder.Descending) return (-compareresult); elsereturn 0;}}

How to use this class is very simple. Create a Windows application, add the listView component listView1 and set it to Details.

转载请注明原文地址:https://www.9cbs.com/read-26531.html

New Post(0)