欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

listview控件实现点击列表头进行listview排序示例分享

程序员文章站 2024-02-23 17:52:40
复制代码 代码如下:using system;using system.collections;using system.windows.forms;namespace c...

复制代码 代码如下:

using system;
using system.collections;
using system.windows.forms;
namespace common
{
    /// <summary>
    /// 对listview点击列标题自动排序功能
    /// </summary>
    public class listviewhelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public listviewhelper()
        {
            //
            // todo: 在此处添加构造函数逻辑
            //
        }
        public static void listview_columnclick(object sender, system.windows.forms.columnclickeventargs e)
        {
            system.windows.forms.listview lv = sender as system.windows.forms.listview;
            // 检查点击的列是不是现在的排序列.
            if (e.column == (lv.listviewitemsorter as listviewcolumnsorter).sortcolumn)
            {
                // 重新设置此列的排序方法.
                if ((lv.listviewitemsorter as listviewcolumnsorter).order == system.windows.forms.sortorder.ascending)
                {
                    (lv.listviewitemsorter as listviewcolumnsorter).order = system.windows.forms.sortorder.descending;
                }
                else
                {
                    (lv.listviewitemsorter as listviewcolumnsorter).order = system.windows.forms.sortorder.ascending;
                }
            }
            else
            {
                // 设置排序列,默认为正向排序
                (lv.listviewitemsorter as listviewcolumnsorter).sortcolumn = e.column;
                (lv.listviewitemsorter as listviewcolumnsorter).order = system.windows.forms.sortorder.ascending;
            }
            // 用新的排序方法对listview排序
            ((system.windows.forms.listview)sender).sort();
        }
    }
    /// <summary>
    /// 继承自icomparer
    /// </summary>
    public class listviewcolumnsorter : system.collections.icomparer
    {
        /// <summary>
        /// 指定按照哪个列排序
        /// </summary>
        private int columntosort;
        /// <summary>
        /// 指定排序的方式
        /// </summary>
        private system.windows.forms.sortorder orderofsort;
        /// <summary>
        /// 声明caseinsensitivecomparer类对象
        /// </summary>
        private system.collections.caseinsensitivecomparer objectcompare;
        /// <summary>
        /// 构造函数
        /// </summary>
        public listviewcolumnsorter()
        {
            // 默认按第一列排序
            columntosort = 0;
            // 排序方式为不排序
            orderofsort = system.windows.forms.sortorder.none;
            // 初始化caseinsensitivecomparer类对象
            objectcompare = new system.collections.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;
            system.windows.forms.listviewitem listviewx, listviewy;
            // 将比较对象转换为listviewitem对象
            listviewx = (system.windows.forms.listviewitem)x;
            listviewy = (system.windows.forms.listviewitem)y;
            string xtext = listviewx.subitems[columntosort].text;
            string ytext = listviewy.subitems[columntosort].text;
            int xint, yint;
            // 比较,如果值为ip地址,则根据ip地址的规则排序。
            if (isip(xtext) && isip(ytext))
            {
                compareresult = compareip(xtext, ytext);
            }
            else if (int.tryparse(xtext, out xint) && int.tryparse(ytext, out yint)) //是否全为数字
            {
                //比较数字
                compareresult = compareint(xint, yint);
            }
            else
            {
                //比较对象
                compareresult = objectcompare.compare(xtext, ytext);
            }
            // 根据上面的比较结果返回正确的比较结果
            if (orderofsort == system.windows.forms.sortorder.ascending)
            {
                // 因为是正序排序,所以直接返回结果
                return compareresult;
            }
            else if (orderofsort == system.windows.forms.sortorder.descending)
            {
                // 如果是反序排序,所以要取负值再返回
                return (-compareresult);
            }
            else
            {
                // 如果相等返回0
                return 0;
            }
        }
        /// <summary>
        /// 判断是否为正确的ip地址,ip范围(0.0.0.0~255.255.255)
        /// </summary>
        /// <param name="ip">需验证的ip地址</param>
        /// <returns></returns>
        public bool isip(string ip)
        {
            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;
        }
        /// <summary>
        /// 比较两个数字的大小
        /// </summary>
        /// <param name="ipx">要比较的第一个对象</param>
        /// <param name="ipy">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        private int compareint(int x, int y)
        {
            if (x > y)
            {
                return 1;
            }
            else if (x < y)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 比较两个ip地址的大小
        /// </summary>
        /// <param name="ipx">要比较的第一个对象</param>
        /// <param name="ipy">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0,如果x大于y返回1,如果x小于y返回-1</returns>
        private int compareip(string ipx, string ipy)
        {
            string[] ipxs = ipx.split('.');
            string[] ipys = ipy.split('.');
            for (int i = 0; i < 4; i++)
            {
                if (convert.toint32(ipxs[i]) > convert.toint32(ipys[i]))
                {
                    return 1;
                }
                else if (convert.toint32(ipxs[i]) < convert.toint32(ipys[i]))
                {
                    return -1;
                }
                else
                {
                    continue;
                }
            }
            return 0;
        }
        /// <summary>
        /// 获取或设置按照哪一列排序.
        /// </summary>
        public int sortcolumn
        {
            set
            {
                columntosort = value;
            }
            get
            {
                return columntosort;
            }
        }
        /// <summary>
        /// 获取或设置排序方式.
        /// </summary>
        public system.windows.forms.sortorder order
        {
            set
            {
                orderofsort = value;
            }
            get
            {
                return orderofsort;
            }
        }
    }
}

以上代码封装后无须更改,在窗体中添加一个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);
}