The
First defines a Listviewhelper class with the following code:
Using System;
Using System.Collections;
Using System.Windows.Forms; namespace Common {///<summary>///ListView Click on the column headers automatically sort function///</summary> public class ListView
Helper {///<summary>///constructor///</summary> public Listviewhelper () {////TODO: Add constructor logic//} public static void Listview_column here Click (object sender, System.Windows.Forms.ColumnClickEventArgs e) {System.Windows.Forms.ListView LV =
sender as System.Windows.Forms.ListView;
Check that the clicked column is not the sorted column now. if (E.column = = (LV). Listviewitemsorter as ListViewColumnSorter).
SortColumn) {//Reset the sorting method for this column. if (LV. Listviewitemsorter as ListViewColumnSorter). Order = = System.Windows.Forms.SortOrder.Ascending) {(LV. Listviewitemsorter as ListViewColumnSorter). Order = SYSTEM.WINDOWS.FORMS.Sortorder.descending; else {(LV. Listviewitemsorter as ListViewColumnSorter).
order = System.Windows.Forms.SortOrder.Ascending; } else {//set row sequence, default to forward sort (LV. Listviewitemsorter as ListViewColumnSorter).
SortColumn = E.column; (LV. Listviewitemsorter as ListViewColumnSorter).
order = System.Windows.Forms.SortOrder.Ascending; ListView sort ((System.Windows.Forms.ListView) sender) with the new sorting method.
Sort (); }///<summary>///inherits from IComparer///</summary> public class Listviewcolumnsorter:syst Em. collections.icomparer {///<summary>///Specify which column to sort by///</summary> private
int columntosort; <summary>///Specifies the sort way///</summary> private System.Windows.Forms.SortOrder ORderofsort; <summary>///Declare caseinsensitivecomparer class object///</summary> private System.collectio Ns.
CaseInsensitiveComparer ObjectCompare;
<summary>///Constructor///</summary> public ListViewColumnSorter () {
The default is sorted by the first column ColumnToSort = 0;
Sort by unordered OrderOfSort = System.Windows.Forms.SortOrder.None;
Initializes the CaseInsensitiveComparer class object objectcompare = new System.Collections.CaseInsensitiveComparer ();
///<summary>///overrides the IComparer interface. </summary>///<param name= "x" > The first object to compare </param>///<param name= "Y" > Second object to compare </param>///<returns> comparison results. If the equality returns 0 if X is greater than Y returns 1 if x is less than y returns -1</returns> public int Compare (ob
Ject x, object y) {int compareresult; System.Windows.Forms.ListViewItem ListViewX, Listviewy;
Converts the comparison object to the ListViewItem object listviewx = (System.Windows.Forms.ListViewItem) x;
Listviewy = (System.Windows.Forms.ListViewItem) y; String xText = Listviewx.subitems[columntosort].
Text; String ytext = Listviewy.subitems[columntosort].
Text;
int Xint, Yint;
Compare, if the value is an IP address, sort according to the rules of the IP address.
if (IsIP (xText) && IsIP (Ytext)) {compareresult = Compareip (XText, Ytext); An else if (int. TryParse (XText, out xint) && Int. TryParse (Ytext, out yint))//are all numbers {//comparison numbers compareresult = Compareint (Xint,
Yint); else {//comparison object compareresult = Objectcompare.compare (XText, Ytext
);
/////Based on the comparison above, returns the correct comparison result if (OrderOfSort = = System.Windows.Forms.SortOrder.Ascending) { //Because it is a positive order, it returns directly to the result return compareresult; else if (OrderOfSort = = System.Windows.Forms.SortOrder.Descending) {//If the order is reversed,
So you want to take negative values and return returns (-compareresult);
else {//if equal returns 0 return 0;
}///<summary>///to determine if it is the correct IP address, IP range (0.0.0.0~255.255.255)///</summary> <param name= "IP" > IP address to be authenticated </param>///<returns></returns> public bool IP (String IP) {return System.Text.RegularExpressions.Regex.Match IP, @ "^" (25[0-5]|2[0-4][0-9]|[ 0-1]{1}[0-9]{2}| [1-9] {1} [0-9] {1}| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [0-9]) $").
Success;
}///<summary> Compare the size of two digits///</summary>///<param name= "IPX" > The first object to compare </param>///<p
Aram Name= "Ipy" > The result of the second object to compare </param>///<returns> comparison. If the equality returns 0, if x is greater than Y returns 1, if x is less than y returns -1</returns>
private int Compareint (int x, int y) {if (x > Y) {return 1;
else if (x < y) {return-1;
else {return 0; }///<summary>///compares the size of two IP addresses///</summary>///<param name= "I PX > The first object to compare </param>///<param name= "Ipy" > The second object to compare </param>///<returns> Comparison of Knots
If the equality returns 0, if x is greater than Y returns 1, if x is less than y returns -1</returns> private int Compareip (string IPX, string ipy) { string[] Ipxs = IPX.
Split ('. '); string[] Ipys = ipy.
Split ('. '); for (int i = 0; I < 4;
i++) {if (Convert.ToInt32 (ipxs[i)) > Convert.ToInt32 (ipys[i)) {
return 1; else if (Convert.ToInt32 (Ipxs[i]) < Convert.ToInt32 (ipys[i)) {R
eturn-1;
else {continue;
} return 0;
///<summary>///Gets or sets which column to sort by. </summary> public int SortColumn {set {ColumnToSort =
Value
get {return columntosort;
}///<summary>///Gets or sets the sort method.
</summary> public System.Windows.Forms.SortOrder Order {set {
OrderOfSort = value;
} get {return orderofsort; }
}
}
}
The above code is encapsulated without change, add a ListView control to the form, and add the following code to the form's Load event:
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.ListViewItemSorter = new Common.ListViewColumnSorter();
this.listView1.ColumnClick += new ColumnClickEventHandler(Common.ListViewHelper.ListView_ColumnClick);
}