.net中的ListView列表控件默认只能实现按第一列排序的功能,而且只能按字符串排序。由于项目需要重写了一个控件。继承自ListVIew,实现了能按每个列排序的功能,而且可以为每一列指定排序方式(数字,字符串,时间)。
完整代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace UserControl
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class myListView : System.Windows.Forms.ListView
{
/// <summary>
/// 必需的设计器变量。
/// 控件说明:
/// 此控件使用一个数组来保存每一列的排序方式。
/// 使用时:当创建了控件的列后,要使用InitTypeList()函数来根据列数生成数组数据。默认值为0,按字符串排序
/// 当有特殊要求时,可以指定相应列所对应的排序值。如typeList[3] = 2;
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("User32")]
private static extern bool LockWindowUpdate(IntPtr hWndLock);
public ArrayList typeList; //存放每一列的排序方式 :0:字符串 1:数字 2:日期
public myListView()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
this.typeList = new ArrayList();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dis