抛砖引玉——实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace LISTVIEWControl
{
    /// <summary>
    /// Summary description for UserControl1.
    /// </summary>
    public class EastSpider : System.Windows.Forms.LISTVIEW
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        ///
        private Comparer myComparer;
        private ArrayList colTypes = new ArrayList();

        private System.ComponentModel.Container components = null;

        /// <summary>
        /// Sort Interface
        /// </summary>

        public class Comparer : IComparer
        {
            private int COLUMN;
            private int sign;
            private EastSpider.COLUMNTypeEnum colType;

            public Comparer()
            {
                COLUMN = 0;
                sign = 1;
                colType = EastSpider.COLUMNTypeEnum.COLUMNTypeText;
            }

            public int COLUMN
            {
                set
                {
                    COLUMN = value;
                }
                get
                {
                    return COLUMN;
                }
            }

            public int Sign
            {
                set
                {
                    sign = value;
                }
                get
                {
                    return sign;
                }
            }

            public COLUMNTypeEnum ColType
            {
                set
                {
                    colType = value;
                }
                get
                {
                    return colType;
                }
            }

            public int Compare(object o1,object o2)
            {
                LISTVIEWItem li1 = (LISTVIEWItem)o1;
                LISTVIEWItem li2 = (LISTVIEWItem)o2;

                string s1,s2;

                try
                {
                    if (COLUMN == 0)
                    {
                        s1 = li1.Text;
                        s2 = li2.Text;

                        //return sign * String.Compare(s1,s2);
                    }

                    else
                    {
                        s1 = li1.SubItems[COLUMN].Text;
                        s2 = li2.SubItems[COLUMN].Text;
                    }

                    if (s1.Length == 0)
                    {
                        if (s2.Length > 0)
                        {
                            return (-sign);
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    else if (s2.Length == 0)
                    {
                        return sign;
                    }

                    switch (colType)
                    {
                        case COLUMNTypeEnum.COLUMNTypeNumber:

                            long d1 = long.Parse(s1);
                            long d2 = long.Parse(s2);

                            if (d1 < d2)
                            {
                                return (-sign);
                            }
                            else if (d1 > d2)
                            {
                                return sign;
                            }
                            else
                            {
                                return 0;
                            }

                            
                        case COLUMNTypeEnum.COLUMNTypeDate:

                            DateTime t1 = DateTime.Parse(s1);
                            DateTime t2 = DateTime.Parse(s2);

                            return sign*DateTime.Compare(t1,t2);

                        default:

                            return sign * String.Compare(s1,s2);

                    }
                }
                catch
                {
                    return (0);
                }
            }
        }

        /// <summary>
        /// 排序类型
        /// </summary>

        [Category("SortType"),Description("The Sort Type of COLUMN you wanted do")]

        public enum COLUMNTypeEnum
        {
            COLUMNTypeText = 0,
            COLUMNTypeDate = 1,
            COLUMNTypeNumber = 2
        }

        /// <summary>
        /// Clear All Items And COLUMNs
        /// </summary>
        private void ClearAll()
        {
            Clear();
            ClearCOLUMNs();
        }

        /// <summary>
        /// Clear All Item
        /// </summary>
        private new void Clear()
        {
            base.Items.Clear();
        }

        /// <summary>
        /// Clear All COLUMNs And ColType
        /// </summary>
        private void ClearCOLUMNs()
        {
            base.COLUMNs.Clear();
            colTypes.Clear();
        }

        public void AddCOLUMN(string caption,COLUMNTypeEnum COLUMNType,HorizontalAlignment ha,int width)
        {
            COLUMNHeader ch = new COLUMNHeader();
            ch.Text = caption;
            ch.TextAlign = ha;
            ch.Width = width;
            base.COLUMNs.Add(ch);
            colTypes.Add(COLUMNType);
        }

        /// <summary>
        /// Add Items
        /// </summary>
        /// <param name="item"></param>
        /// <param name="bgcolor"></param>
        /// <returns></returns>

        public int AddRow(LISTVIEWItem item,Color bgcolor)
        {
            //LISTVIEWItem li = new LISTVIEWItem(args);
            item.UseItemStyleForSubItems = true;
            item.BackColor = bgcolor;
            Items.Add(item);
            return item.Index;
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param> 共2页: 上一页 1 [2] 下一页 <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
上一篇用PHP模拟C的数据结构
下一篇怎样在PHP中通过ADO调用Asscess数据库和COM程序
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭