[點(diǎn)晴永久免費(fèi)OA]在C#中實(shí)現(xiàn)對(duì)ListView點(diǎn)擊列標(biāo)題自動(dòng)排序功能
首先定義一個(gè)ListViewHelper類,代碼如下:
using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
namespace Common
...{
/**//// <summary>
/// 模塊編號(hào):
/// 模塊功能:對(duì)ListView的封裝
/// 作 者:雷友兵
/// 編寫時(shí)間:2007-1-10
/// </summary>
public class ListViewHelper
...{
/**//// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public ListViewHelper()
...{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public static void ListView_ColumnClick(object sender, ColumnClickEventArgs e)
...{
ListView lv = sender as ListView;
// 檢查點(diǎn)擊的列是不是現(xiàn)在的排序列.
if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn)
...{
// 重新設(shè)置此列的排序方法.
if ((lv.ListViewItemSorter as ListViewColumnSorter).Order == SortOrder.Ascending)
...{
(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Descending;
}
else
...{
(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
}
}
else
...{
// 設(shè)置排序列,默認(rèn)為正向排序
(lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
(lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
}
// 用新的排序方法對(duì)ListView排序
((ListView)sender).Sort();
}
}
/**//// <summary>
/// 繼承自IComparer
/// </summary>
public class ListViewColumnSorter : IComparer
...{
/**//// <summary>
/// 指定按照哪個(gè)列排序
/// </summary>
private int ColumnToSort;
/**//// <summary>
/// 指定排序的方式
/// </summary>
private SortOrder OrderOfSort;
/**//// <summary>
/// 聲明CaseInsensitiveComparer類對(duì)象,
/// 參見ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemCollectionsCaseInsensitiveComparerClassTopic.htm
/// </summary>
private CaseInsensitiveComparer ObjectCompare;
/**//// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public ListViewColumnSorter()
...{
// 默認(rèn)按第一列排序
ColumnToSort = 0;
// 排序方式為不排序
OrderOfSort = SortOrder.None;
// 初始化CaseInsensitiveComparer類對(duì)象
ObjectCompare = new CaseInsensitiveComparer();
}
/**//// <summary>
/// 重寫IComparer接口.
/// </summary>
/// <param name="x">要比較的第一個(gè)對(duì)象</param>
/// <param name="y">要比較的第二個(gè)對(duì)象</param>
/// <returns>比較的結(jié)果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
public int Compare(object x, object y)
...{
int compareResult;
ListViewItem listviewX, listviewY;
// 將比較對(duì)象轉(zhuǎn)換為L(zhǎng)istViewItem對(duì)象
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;
// 比較
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
// 根據(jù)上面的比較結(jié)果返回正確的比較結(jié)果
if (OrderOfSort == SortOrder.Ascending)
...{
// 因?yàn)槭钦蚺判颍灾苯臃祷亟Y(jié)果
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
...{
// 如果是反序排序,所以要取負(fù)值再返回
return (-compareResult);
}
else
...{
// 如果相等返回0
return 0;
}
}
/**//// <summary>
/// 獲取或設(shè)置按照哪一列排序.
/// </summary>
public int SortColumn
...{
set
...{
ColumnToSort = value;
}
get
...{
return ColumnToSort;
}
}
/**//// <summary>
/// 獲取或設(shè)置排序方式.
/// </summary>
public SortOrder Order
...{
set
...{
OrderOfSort = value;
}
get
...{
return OrderOfSort;
}
}
}
} 以上代碼封裝后無須更改,在窗體中添加一個(gè)ListView控件,在窗體的Load事件中添加如下代碼: private void Form1_Load( object sender, EventArgs e) { this .listView1.ListViewItemSorter = new Common.ListViewColumnSorter(); this .listView1.ColumnClick += new ColumnClickEventHandler(Common.ListViewHelper.ListView_ColumnClick); } 該文章在 2022/12/22 20:11:59 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |