Example code sharing to implement C#listview click column header Ordering

Source: Internet
Author: User
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); }   }  }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.