C# listview 单击列头实现排序 <二>

单击列头实现排序,首先在羡慕中添加下面的帮助实现的类;具体的代码:

 

  1 using System;
  2 using System.Collections;
  3 using System.Windows.Forms;
  4 namespace Common
  5 {
  6     /// <summary>
  7     /// 对ListView点击列标题自动排序功能
  8     /// </summary>
  9     public class ListViewHelper
 10     {
 11         /// <summary>
 12         /// 构造函数
 13         /// </summary>
 14         public ListViewHelper()
 15         {
 16             //
 17             // TODO: 在此处添加构造函数逻辑
 18             //
 19         }
 20         public static void ListView_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
 21         {
 22             System.Windows.Forms.ListView lv = sender as System.Windows.Forms.ListView;
 23             // 检查点击的列是不是现在的排序列.
 24             if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn)
 25             {
 26                 // 重新设置此列的排序方法.
 27                 if ((lv.ListViewItemSorter as ListViewColumnSorter).Order == System.Windows.Forms.SortOrder.Ascending)
 28                 {
 29                     (lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Descending;
 30                 }
 31                 else
 32                 {
 33                     (lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Ascending;
 34                 }
 35             }
 36             else
 37             {
 38                 // 设置排序列,默认为正向排序
 39                 (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
 40                 (lv.ListViewItemSorter as ListViewColumnSorter).Order = System.Windows.Forms.SortOrder.Ascending;
 41             }
 42             // 用新的排序方法对ListView排序
 43             ((System.Windows.Forms.ListView)sender).Sort();
 44         }
 45     }
 46     /// <summary>
 47     /// 继承自IComparer
 48     /// </summary>
 49     public class ListViewColumnSorter : System.Collections.IComparer
 50     {
 51         /// <summary>
 52         /// 指定按照哪个列排序
 53         /// </summary>
 54         private int ColumnToSort;
 55         /// <summary>
 56         /// 指定排序的方式
 57         /// </summary>
 58         private System.Windows.Forms.SortOrder OrderOfSort;
 59         /// <summary>
 60         /// 声明CaseInsensitiveComparer类对象
 61         /// </summary>
 62         private System.Collections.CaseInsensitiveComparer ObjectCompare;
 63         /// <summary>
 64         /// 构造函数
 65         /// </summary>
 66         public ListViewColumnSorter()
 67         {
 68             // 默认按第一列排序
 69             ColumnToSort = 0;
 70             // 排序方式为不排序
 71             OrderOfSort = System.Windows.Forms.SortOrder.None;
 72             // 初始化CaseInsensitiveComparer类对象
 73             ObjectCompare = new System.Collections.CaseInsensitiveComparer();
 74         }
 75         /// <summary>
 76         /// 重写IComparer接口.
 77         /// </summary>
 78         /// <param name="x">要比较的第一个对象</param>
 79         /// <param name="y">要比较的第二个对象</param>
 80         /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
 81         public int Compare(object x, object y)
 82         {
 83             int compareResult;
 84             System.Windows.Forms.ListViewItem listviewX, listviewY;
 85             // 将比较对象转换为ListViewItem对象
 86             listviewX = (System.Windows.Forms.ListViewItem)x;
 87             listviewY = (System.Windows.Forms.ListViewItem)y;
 88             string xText = listviewX.SubItems[ColumnToSort].Text;
 89             string yText = listviewY.SubItems[ColumnToSort].Text;
 90             int xInt, yInt;
 91             // 比较,如果值为IP地址,则根据IP地址的规则排序。
 92             if (IsIP(xText) && IsIP(yText))
 93             {
 94                 compareResult = CompareIp(xText, yText);
 95             }
 96             else if (int.TryParse(xText, out xInt) && int.TryParse(yText, out yInt)) //是否全为数字
 97             {
 98                 //比较数字
 99                 compareResult = CompareInt(xInt, yInt);
100             }
101             else
102             {
103                 //比较对象
104                 compareResult = ObjectCompare.Compare(xText, yText);
105             }
106             // 根据上面的比较结果返回正确的比较结果
107             if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
108             {
109                 // 因为是正序排序,所以直接返回结果
110                 return compareResult;
111             }
112             else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
113             {
114                 // 如果是反序排序,所以要取负值再返回
115                 return (-compareResult);
116             }
117             else
118             {
119                 // 如果相等返回0
120                 return 0;
121             }
122         }
123         /// <summary>
124         /// 判断是否为正确的IP地址,IP范围(0.0.0.0~255.255.255)
125         /// </summary>
126         /// <param name="ip">需验证的IP地址</param>
127         /// <returns></returns>
128         public bool IsIP(String ip)
129         {
130             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;
131         }
132         /// <summary>
133         /// 比较两个数字的大小
134         /// </summary>
135         /// <param name="ipx">要比较的第一个对象</param>
136         /// <param name="ipy">要比较的第二个对象</param>
137         /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
138         private int CompareInt(int x, int y)
139         {
140             if (x > y)
141             {
142                 return 1;
143             }
144             else if (x < y)
145             {
146                 return -1;
147             }
148             else
149             {
150                 return 0;
151             }
152         }
153         /// <summary>
154         /// 比较两个IP地址的大小
155         /// </summary>
156         /// <param name="ipx">要比较的第一个对象</param>
157         /// <param name="ipy">要比较的第二个对象</param>
158         /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
159         private int CompareIp(string ipx, string ipy)
160         {
161             string[] ipxs = ipx.Split('.');
162             string[] ipys = ipy.Split('.');
163             for (int i = 0; i < 4; i++)
164             {
165                 if (Convert.ToInt32(ipxs[i]) > Convert.ToInt32(ipys[i]))
166                 {
167                     return 1;
168                 }
169                 else if (Convert.ToInt32(ipxs[i]) < Convert.ToInt32(ipys[i]))
170                 {
171                     return -1;
172                 }
173                 else
174                 {
175                     continue;
176                 }
177             }
178             return 0;
179         }
180         /// <summary>
181         /// 获取或设置按照哪一列排序.
182         /// </summary>
183         public int SortColumn
184         {
185             set
186             {
187                 ColumnToSort = value;
188             }
189             get
190             {
191                 return ColumnToSort;
192             }
193         }
194         /// <summary>
195         /// 获取或设置排序方式.
196         /// </summary>
197         public System.Windows.Forms.SortOrder Order
198         {
199             set
200             {
201                 OrderOfSort = value;
202             }
203             get
204             {
205                 return OrderOfSort;
206             }
207         }
208     }
209 }

以上代码封装后无须更改,在窗体中添加一个ListView控件,在窗体的Load事件中添加如下代码:

1  private void Form1_Load(object sender, EventArgs e)
2  {
3      this.listView1.ListViewItemSorter = new Common.ListViewColumnSorter();
4      this.listView1.ColumnClick += new ColumnClickEventHandler(Common.ListViewHelper.ListView_ColumnClick);
5  }

 

posted @ 2017-01-04 15:41  FelixWang  阅读(2572)  评论(0编辑  收藏  举报