Custom Sorting Features of ListView Controls

xiaoxiao2021-03-06  41

In software development, use ListView to customize, how can some classes to do, strings, integers, date and other types? 1. Creating a ListViewItemComparer class Create a ListViewItemComparer class The following custom two classes are inherited from IComparer, one for string sorting, one for integer sorting ///

/// String sort /// < / summary> public class ListViewItemComparer: IComparer {private int col; public ListViewItemComparer () {col = 0;} public ListViewItemComparer (int column) {col = column;} public int Compare (object x, object y) {int ret = String .Compare ((ListViewItem) x) .SUBITEMS [col] .text, ((ListViewItem) Y) .SUBITEMS [col] .text); Return Ret;}} /// /// Sort /// public class ListViewIntItemComparer: IComparer {private int col; public ListViewIntItemComparer () {col = 0;} public ListViewIntItemComparer (int column) {col = column;} public int Compare (object x, object y) {INT LVX = 0; int LVY = 0; try {lvx = int32.parse ((ListViewItem) x) .SUBITEMS [col] .text); lvy = int32.parse ((listviewItem) Y) .SUBITEMS [col ] .Text);} catCH (Exception Exp) {RETURN-1;} IF (lvx> lvy) Return 1; Else Return 0; }} II ColumnClick event processing in the ListView: private void listView2_ColumnClick (object sender, System.Windows.Forms.ColumnClickEventArgs e) {// Set the ListViewItemSorter property to a new ListViewItemComparer object this.listView2.ListViewItemSorter = new ListViewIntItemComparer (e. .Column); // Call the sort method to manually sort the column based on the listviewItemComparer implement ();} MSDN / library / netframework / netframework / nfdnwinformslistviewsort.mspx

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

New Post(0)