• 支持多种数据类型的ListView排序


    这是我写的一个ListView排序的例子。以前在网上看到的大多只是支持字符串排序,但是总体思想是一样的。

    1   先写一个Comparer类  ListViewItemComparer.
         属性 Column 用来记载要进行排列的ListView列的索引。这个属性在执行ColumnClick事件时   通过ColumnClickEventArgs参数赋值。
        属性 SortOrder 用来描述排序模式:正序,反序。
        属性 SortedType 用来描述是那中数据类型排序,这个枚举包括:Date(日期),Number(数字),Char(字符串),Blend(数字字符串混合类型)


    using System;
    using System.Collections;
    using System.IO;
    using System.Windows.Forms;

    namespace Windy.Command.WinControl.ListView
    {
        /**//// <summary>
        /// Summary description for ListViewItemComparer.
        /// </summary>
        public class ListViewItemComparer : IComparer
        {
            public ListViewItemComparer()
            {
                 
            }

            /**//// <summary>
            /// 
            /// </summary>
            private int column = 0;
            /**//// <summary>
            /// 
            /// </summary>
            private SortOrder order;
            /**//// <summary>
            /// 
            /// </summary>
            private SortedType sortedtype;


            /**//// <summary>
            /// 
            /// </summary>
            public int Column
            {
                get
                {
                    return column;
                }
                set 
                {
                    column=value;
                }
            }
            /**//// <summary>
            ///  
            /// </summary>
            public SortOrder Order
            {
                get 
                {
                    return order;
                }
                set
                {
                    order=value;
                }
            }

            public SortedType SortType
            {
                get
                {
                    return sortedtype;
                }
                set
                {
                    sortedtype = value;
                }
            }



            public int Compare(object x, object y)
            {
                int retval = 0;

                string sx = "";
                string sy = "";

                ListViewItem lisviewX = (ListViewItem)x;
                ListViewItem lisviewY = (ListViewItem)y;
                
                if (column>=lisviewX.SubItems.Count)
                {
                    sx = "";
                }
                else
                {
                    sx = ( (ListViewItem)x ).SubItems[column].Text;
                    
                }
                if (column>=lisviewY.SubItems.Count)
                {
                    sy = "";
                }
                else
                {
                    sy = ( (ListViewItem)y ).SubItems[column].Text;
                }

                switch(SortType)
                {
                
                    case SortedType.Blend:
                    {
                        char[] chrx = sx.ToCharArray();
                        char[] chry = sy.ToCharArray();

                        int charXValueCount = 0;
                        int charYValueCount = 0;

                        foreach( char c in chrx )
                        {
                            charXValueCount += c;
                        }

                        foreach( char c in chry )
                        {
                            charYValueCount += c;
                        }

                        if ( charXValueCount > charYValueCount )
                        {
                            retval = 1;
                        }
                        else 
                        {
                            retval = -1;
                        }

                    }
                        break;
                    case SortedType.Char:
                    {
                        try
                        {
                            retval = String.Compare(sx,sy);
                        }
                        catch
                        {
                          retval = Comparer.Default.Compare(x,y);
                        }
                        
                    }
                        break;
                    case SortedType.Date:
                    {
                        try
                        {
                            DateTime dtx = Convert.ToDateTime(sx);
                            DateTime dty = Convert.ToDateTime(sy);
                            retval = DateTime.Compare(dtx,dty);    
                        }
                        catch
                        {
                            retval = String.Compare(sx,sy);
                        }
        
                    }
                        break;
                    case SortedType.Number:
                    {

                        try
                        {
                            Decimal dx = Convert.ToDecimal(sx);
                            Decimal dy = Convert.ToDecimal(sy);
                            retval = Decimal.Compare(dx,dy);
                            
                        }
                        catch
                        {
                            retval = Comparer.Default.Compare(sx,sy);
                        }

                    }
                    break;
                
                default:
                break;
                
                }

                        
                if( order == SortOrder.Ascending )
                {
                    return retval;
                }
                else if( order==SortOrder.Descending )
                {
                    return -retval;
                }
                else
                {
                    return 0;
                }
            
            }

        }

        /**//// <summary>
        /// 
        /// </summary>
        public enum SortedType
        {
            Date,Number,Char,Blend
        }
    }
    2 调用的时候:

    先创建ListViewItemComparer 实体:

    ListViewItemComparer comparer =  new ListViewItemComparer();

    设置ListView 的 ListViewItemSorter属性为 comparer 。
    处理ListView的ColumnClick事件:
       这里唯一需要做的就是把你想要排序的列的数据类型“人为”
     分析出来,然后给comparer 的SortType 属性赋相应的值。最后
    执行ListView的Sort()方法。

     PS:代码没写注解,如有疑问和我联系。

    private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
            {
                if(comparer.Order==SortOrder.Ascending)
                {
                    comparer.Order = SortOrder.Descending;
                }
                else if(comparer.Order==SortOrder.Descending)
                {
                    comparer.Order = SortOrder.Ascending;
                }

                comparer.Column = e.Column;
                if (comparer.Column == 0)
                {
                    comparer.SortType = SortedType.Blend;
                }
                else if (comparer.Column == 1)
                {
                    comparer.SortType = SortedType.Char;
                }
                else if (comparer.Column == 2)
                {
                    comparer.SortType = SortedType.Number;
                }
                else
                {
                    comparer.SortType = SortedType.Date;
                }
                listView1.Sort();
             }
  • 相关阅读:
    让程序的性能提升10倍
    以正确的姿势实现一棵JavaScript菜单树
    利用php数组函数进行函数式编程
    yum安装下的nginx,如何添加模块,和添加第三方模块
    前端面试题 vue
    前端常用面试题目及答案-HTML&CSS篇
    使用after伪类,配合IE的zoom或者overflow清除浮动
    函数式编程
    在CentOS 7上安装Node.js
    node中__dirname、__filename、process.cwd()、process.chdir()表示的路径
  • 原文地址:https://www.cnblogs.com/windyliu/p/803075.html
Copyright © 2020-2023  润新知