listview control to click the list header for listview sorting sample sharing
- 2020-06-15 10:09:20
- OfStack
using System;
using System.Collections;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// right ListView Click on the auto-sort function of column headings
/// </summary>
public class ListViewHelper
{
/// <summary>
/// The constructor
/// </summary>
public ListViewHelper()
{
//
// TODO: Add constructor logic here
//
}
public static void ListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
System.Windows.Forms.ListView lv = sender as System.Windows.Forms.ListView;
// Check to see if the clicked column is now sorted .
if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn)
{
// Resets 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
{
// Sets the sorting sequence, which defaults to forward sorting
(lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
(lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Ascending;
}
// Using the new sorting method ListView The sorting
((System.Windows.Forms.ListView)sender).Sort();
}
}
/// <summary>
/// Inherited from IComparer
/// </summary>
public class ListViewColumnSorter : System.Collections.IComparer
{
/// <summary>
/// Specifies which column to sort by
/// </summary>
private int ColumnToSort;
/// <summary>
/// Specifies how to sort
/// </summary>
private System.Windows.Forms.SortOrder OrderOfSort;
/// <summary>
/// The statement CaseInsensitiveComparer Class object
/// </summary>
private System.Collections.CaseInsensitiveComparer ObjectCompare;
/// <summary>
/// The constructor
/// </summary>
public ListViewColumnSorter()
{
// The default according to the first 1 Column sorting
ColumnToSort = 0;
// Sort by no sort
OrderOfSort = System.Windows.Forms.SortOrder.None;
// Initialize the CaseInsensitiveComparer Class object
ObjectCompare = new System.Collections.CaseInsensitiveComparer();
}
/// <summary>
/// rewrite IComparer interface .
/// </summary>
/// <param name="x"> To compare 1 An object </param>
/// <param name="y"> To compare 2 An object </param>
/// <returns> Results of comparison . If equal returns 0 If the x Is greater than y return 1 If the x Less than y return -1</returns>
public int Compare(object x, object y)
{
int compareResult;
System.Windows.Forms.ListViewItem listviewX, listviewY;
// Converts the comparison object to 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;
// To compare , If the value is IP Address, then according to IP The rule of address ordering.
if (IsIP(xText) && IsIP(yText))
{
compareResult = CompareIp(xText, yText);
}
else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) // Is it all Numbers
{
// Comparing the digital
compareResult = CompareInt(xInt, yInt);
}
else
{
// Comparison of object
compareResult = ObjectCompare.Compare(xText, yText);
}
// Return the correct comparison based on the comparison above
if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
{
// Because it is sorted in positive order, the result is returned directly
return compareResult;
}
else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
{
// If it's in reverse order, you have to take a negative value and return it
return (-compareResult);
}
else
{
// If equal returns 0
return 0;
}
}
/// <summary>
/// Is it correct IP The address, IP Range ( 0.0.0.0 ~ 255.255.255 )
/// </summary>
/// <param name="ip"> To verify the IP address </param>
/// <returns></returns>
public bool IsIP(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 Numbers
/// </summary>
/// <param name="ipx"> To compare 1 An object </param>
/// <param name="ipy"> To compare 2 An object </param>
/// <returns> Results of comparison . If equal returns 0 If the x Is greater than y return 1 If the x Less than y return -1</returns>
private int CompareInt(int x, int y)
{
if (x > y)
{
return 1;
}
else if (x < y)
{
return -1;
}
else
{
return 0;
}
}
/// <summary>
/// Compare the two IP Address size
/// </summary>
/// <param name="ipx"> To compare 1 An object </param>
/// <param name="ipy"> To compare 2 An object </param>
/// <returns> Results of comparison . If equal returns 0 If the x Is greater than y return 1 If the x Less than y return -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]))
{
return -1;
}
else
{
continue;
}
}
return 0;
}
/// <summary>
/// Gets or sets according to which 1 Column sorting .
/// </summary>
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}
/// <summary>
/// Gets or sets the sort mode .
/// </summary>
public System.Windows.Forms.SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}
}
}
Add 1 ListView control to the form and the following code to the Load event of the form:
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.ListViewItemSorter = new Common.ListViewColumnSorter();
this.listView1.ColumnClick += new ColumnClickEventHandler(Common.ListViewHelper.ListView_ColumnClick);
}