Here is a small series for everyone to bring a C # ListView click the column header to sort the instance. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Examples are as follows:
#region custom variable int currentcol =-1; BOOL sort; #endregion//Column header click event private void Lvw_columnclick (object sender, Columnclickeventargs e) {String Asc = ((char) 0X25BC). ToString (). PadLeft (4, "); String Des = ((char) 0x25b2). ToString (). PadLeft (4, "); if (sort = false) {sort = true; String oldstr = This.lvw.columns[e.column]. Text.trimend (char) 0X25BC, (char) 0x25b2, "); This.lvw.columns[e.column]. Text = Oldstr + Des; } else if (sort = true) {sort = false; String oldstr = This.lvw.columns[e.column]. Text.trimend (char) 0X25BC, (char) 0x25b2, "); This.lvw.columns[e.column]. Text = Oldstr + Asc; } if (LVW. Columns[e.column]. tag.tostring () = = "n")//The column header tag is set to "N" in the designer, which means that the column is processed by a digital comparator, otherwise the text is LVW. Listviewitemsorter = new Listviewitemcomparernum (e.column, sort); Else LVW. Listviewitemsorter = new ListViewItemComparer (e.column, sort); This.lvw.Sort (); int rowCount = This.lvw.Items.Count; if (currentcol! =-1) {if (E.column! = Currentcol) this.Lvw. Columns[currentcol]. Text = This.lvw.columns[currentcol]. Text.trimend (char) 0X25BC, (char) 0x25b2, "); } Currentcol = E.column; }//Text Comparator public class Listviewitemcomparer:icomparer {public bool sort_b; Public SortOrder order = sortorder.ascending; private int col; Public ListViewItemComparer () {col = 0; } public listviewitemcomparer (int column, bool sort) {col = column; Sort_b = sort; } public int Compare (object x, object y) {if (sort_b) {return String.Compare (((ListViewItem) x). Subitems[col]. Text, ((ListViewItem) y). Subitems[col]. Text); } else {return String.Compare (((ListViewItem) y). Subitems[col]. Text, ((ListViewItem) x). Subitems[col]. Text); }}}//Digital comparator public class Listviewitemcomparernum:icomparer {public bool sort_b; Public SortOrder order = sortorder.ascending; private int col; Public Listviewitemcomparernum () {col = 0; } public listviewitemcomparernum (int column, bool sort) { col = column; Sort_b = sort; } public int Compare (object x, Object y) {decimal d1=convert.todecimal ((ListViewItem) x). Subitems[col]. Text); Decimal D2=convert.todecimal (((ListViewItem) y). Subitems[col]. Text); if (sort_b) {return decimal.compare (D1,D2); } else {return decimal.compare (D2,D1); } } }