c# listview排序功能_c#入门

2022-11-09 15:58:27 浏览数 (2)

ListViewColumnSorter.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Windows.Forms;

namespace Microblue { class ListViewColumnSorter : IComparer { /**/ /// <summary> /// 指定按照哪个列排序 /// </summary> private int ColumnToSort; /**/ /// <summary> /// 指定排序的方式 /// </summary> private SortOrder OrderOfSort; /**/ /// <summary> /// 声明CaseInsensitiveComparer类对象, /// 参见ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemCollectionsCaseInsensitiveComparerClassTopic.htm /// </summary> private CaseInsensitiveComparer ObjectCompare;

/**/ /// <summary> /// 构造函数 /// </summary> public ListViewColumnSorter() { // 默认按第一列排序 ColumnToSort = 0;

// 排序方式为不排序 OrderOfSort = SortOrder.None;

// 初始化CaseInsensitiveComparer类对象 ObjectCompare = new CaseInsensitiveComparer(); }

/**/ /// <summary> /// 重写IComparer接口. /// </summary> /// <param name=”x”>要比较的第一个对象</param> /// <param name=”y”>要比较的第二个对象</param> /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns> public int Compare(object x, object y) { int compareResult; ListViewItem listviewX, listviewY;

// 将比较对象转换为ListViewItem对象 listviewX = (ListViewItem)x; listviewY = (ListViewItem)y;

// 比较 compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

// 根据上面的比较结果返回正确的比较结果 if (OrderOfSort == SortOrder.Ascending) { // 因为是正序排序,所以直接返回结果 return compareResult; } else if (OrderOfSort == SortOrder.Descending) { // 如果是反序排序,所以要取负值再返回 return (-compareResult); } else { // 如果相等返回0 return 0; } }

/**/ /// <summary> /// 获取或设置按照哪一列排序. /// </summary> public int SortColumn { set { ColumnToSort = value; } get { return ColumnToSort; } }

/**/ /// <summary> /// 获取或设置排序方式. /// </summary> public SortOrder Order {

set { OrderOfSort = value; } get { return OrderOfSort; } } } }

调用顺序排序ListViewColumnSorter类1:

private ListViewColumnSorter lvwColumnSorter;

//listView1事件 private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) { // 创建一个ListView排序类的对象,并设置listView1的排序器 lvwColumnSorter = new ListViewColumnSorter();

this.listView1.ListViewItemSorter = lvwColumnSorter;

if (e.Column == lvwColumnSorter.SortColumn) { // 重新设置此列的排序方法. if (lvwColumnSorter.Order == SortOrder.Ascending) { listView1.Sorting = SortOrder.Descending; lvwColumnSorter.Order = SortOrder.Descending; } else { listView1.Sorting = SortOrder.Ascending; //lvwColumnSorter.Order = SortOrder.Ascending; } } else { // 设置排序列,默认为正向排序 lvwColumnSorter.SortColumn = e.Column; if (lvwColumnSorter.Order == SortOrder.Ascending) { listView1.Sorting = SortOrder.Descending; //lvwColumnSorter.Order = SortOrder.Descending; } else { listView1.Sorting = SortOrder.Ascending; //lvwColumnSorter.Order = SortOrder.Ascending; } //lvwColumnSorter.Order = SortOrder.Ascending; }

// 用新的排序方法对ListView排序 //this.listView1.Sort();

}

调用逆序和顺序排序ListViewColumnSorter类2:

private ListViewColumnSorter lvwColumnSorter; private bool sort = false;

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) { // 创建一个ListView排序类的对象,并设置listView1的排序器 lvwColumnSorter = new ListViewColumnSorter();

if (sort == false) { sort = true; lvwColumnSorter.Order = SortOrder.Descending; } else { sort = false; lvwColumnSorter.Order = SortOrder.Ascending; } lvwColumnSorter.SortColumn = e.Column; this.listView1.ListViewItemSorter = lvwColumnSorter;

// 用新的排序方法对ListView排序 //this.listView1.Sort();

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/186225.html原文链接:https://javaforall.cn

0 人点赞