• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#ListView点击列头进行排序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

  1  /// <summary>
  2     /// This class is an implementation of the 'IComparer' interface.
  3     /// </summary>
  4     public class ListViewColumnSorter : IComparer
  5     {
  6         /// <summary>
  7         /// Specifies the column to be sorted
  8         /// </summary>
  9         private int ColumnToSort;
 10         /// <summary>
 11         /// Specifies the order in which to sort (i.e. 'Ascending').
 12         /// </summary>
 13         private SortOrder OrderOfSort;
 14         /// <summary>
 15         /// Case insensitive comparer object
 16         /// </summary>
 17         private CaseInsensitiveComparer ObjectCompare;
 18 
 19         /// <summary>
 20         /// Class constructor.  Initializes various elements
 21         /// </summary>
 22         public ListViewColumnSorter()
 23         {
 24             // Initialize the column to '0'
 25             ColumnToSort = 0;
 26 
 27             // Initialize the sort order to 'none'
 28             OrderOfSort = SortOrder.None;
 29 
 30             // Initialize the CaseInsensitiveComparer object
 31             ObjectCompare = new CaseInsensitiveComparer();
 32         }
 33 
 34         private int comaretInt = 0;
 35         /// <summary>
 36         /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
 37         /// </summary>
 38         /// <param name="x">First object to be compared</param>
 39         /// <param name="y">Second object to be compared</param>
 40         /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
 41         public int Compare(object x, object y)
 42         {
 43             int compareResult;
 44             ListViewItem listviewX, listviewY;
 45 
 46             // Cast the objects to be compared to ListViewItem objects
 47             listviewX = (ListViewItem)x;
 48             listviewY = (ListViewItem)y;
 49 
 50             if (int.TryParse(listviewX.SubItems[ColumnToSort].Text, out comaretInt) &&
 51                int.TryParse(listviewY.SubItems[ColumnToSort].Text, out comaretInt))
 52             {
 53                 compareResult = int.Parse(listviewX.SubItems[ColumnToSort].Text).CompareTo(int.Parse(listviewY.SubItems[ColumnToSort].Text));
 54             }
 55             else
 56             {
 57                 // Compare the two items
 58                 compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
 59             }
 60             // Calculate correct return value based on object comparison
 61             if (OrderOfSort == SortOrder.Ascending)
 62             {
 63                 // Ascending sort is selected, return normal result of compare operation
 64                 return compareResult;
 65             }
 66             else if (OrderOfSort == SortOrder.Descending)
 67             {
 68                 // Descending sort is selected, return negative result of compare operation
 69                 return (-compareResult);
 70             }
 71             else
 72             {
 73                 // Return '0' to indicate they are equal
 74                 return 0;
 75             }
 76         }
 77 
 78         /// <summary>
 79         /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
 80         /// </summary>
 81         public int SortColumn
 82         {
 83             set
 84             {
 85                 ColumnToSort = value;
 86             }
 87             get
 88             {
 89                 return ColumnToSort;
 90             }
 91         }
 92 
 93         /// <summary>
 94         /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
 95         /// </summary>
 96         public SortOrder Order
 97         {
 98             set
 99             {
100                 OrderOfSort = value;
101             }
102             get
103             {
104                 return OrderOfSort;
105             }
106         }
107 
108     }

 

将上面的类添加到你的项目里面。

 1  private ListViewColumnSorter lvwColumnSorter =  new ListViewColumnSorter();
 2           
 3 
 4         void listTask_ColumnClick(object sender, ColumnClickEventArgs e)
 5         {
 6             foreach (ColumnHeader ch in listTask.Columns)
 7             {
 8                 ch.ImageIndex = -1;
 9             }
10 
11             listTask.ListViewItemSorter = lvwColumnSorter;
12 
13             //if (listTask.SmallImageList == null)
14             //{
15             //    listTask.SmallImageList = imageList1;
16             //}
17 
18             //listTask.Columns[e.Column].ImageIndex = 0;
19 
20             if (e.Column == lvwColumnSorter.SortColumn)
21             {
22                 // Reverse the current sort direction for this column.
23                 if (lvwColumnSorter.Order == SortOrder.Ascending)
24                 {
25                     lvwColumnSorter.Order = SortOrder.Descending;
26                     //listTask.Columns[e.Column].ImageIndex = 1;
27                 }
28                 else
29                 {
30                     lvwColumnSorter.Order = SortOrder.Ascending;
31                 }
32             }
33             else
34             {
35                 // Set the column number that is to be sorted; default to ascending.
36                 lvwColumnSorter.SortColumn = e.Column;
37                 lvwColumnSorter.Order = SortOrder.Ascending;
38             }
39 
40             // Perform the sort with these new sort options.
41 
42             this.listTask.Sort();
43             listTask.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);   //根据列的文本内容,自动设置list列的宽度
44             listTask.Update();
45 
46         }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
abstractvirtual在C++和C#中的比较发布时间:2022-07-13
下一篇:
C#SeleniumExpectedConditions不存在发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap