1. 程式人生 > >ListView 列標頭排序

ListView 列標頭排序

在 msdn 示例上改的,可以對數字排序:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ListView listview = new ListView();
        public Form1()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(400, 300);
            listview.Dock = DockStyle.Fill;
            listview.Parent = this;
            
            //詳細檢視
            listview.View = View.Details;
            //選擇總行
            listview.FullRowSelect = true;
            //設定 listview 的排序方法,在列標頭事件中可以取出用來判斷
            listview.Sorting = SortOrder.Ascending;
            //設定按第1列排序
            listview.ListViewItemSorter = new ListViewItemComparer(listview.Sorting, 1);

            listview.Columns.Add("名稱", 80, HorizontalAlignment.Left);
            listview.Columns.Add("排名", 50, HorizontalAlignment.Center);
            listview.Columns.Add("描述", 200, HorizontalAlignment.Left);

            //新增測試資料
            ListViewItem lvi = new ListViewItem("AAA");
            lvi.SubItems.Add("1");
            lvi.SubItems.Add("哎,我是AAA");
            listview.Items.Add(lvi);

            lvi = new ListViewItem("BBB");
            lvi.SubItems.Add("2");
            lvi.SubItems.Add("啊,我是BBB");
            listview.Items.Add(lvi);

            lvi = new ListViewItem("CCC");
            lvi.SubItems.Add("3");
            lvi.SubItems.Add("這是CCC");
            listview.Items.Add(lvi);

            //新增列標題單擊事件方法
            listview.ColumnClick += Listview_ColumnClick;
        }


        // listview 列標頭單擊事件方法。
        private void Listview_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (listview.Sorting == SortOrder.Ascending)
            {
                listview.ListViewItemSorter = new ListViewItemComparer(SortOrder.Descending, e.Column);
                listview.Sorting = SortOrder.Descending;
            }
            else
            {
                listview.ListViewItemSorter = new ListViewItemComparer(SortOrder.Ascending, e.Column);
                listview.Sorting = SortOrder.Ascending;
            }
        }
    }


    //定義一個類,這個類實現 IComparer 介面
    class ListViewItemComparer : IComparer
    {
        private int col;
        private SortOrder sort = SortOrder.None;
        public ListViewItemComparer(SortOrder sortOrder)
        {
            col = 0;
        }
        public ListViewItemComparer(SortOrder sortOrder, int column)
        {
            sort = sortOrder;
            col = column;
        }

        //實現介面的 Compare 方法,x、y 為要比較的兩個物件 
        public int Compare(object x, object y)
        {
            //預設升序。判斷傳入的排序列舉,如果為降序就對換要比較的物件
            if (sort == SortOrder.Descending)
            {
                object temp = x;
                x = y;
                y = temp;
            }

            string xx = ((ListViewItem)x).SubItems[col].Text;
            string yy = ((ListViewItem)y).SubItems[col].Text;

            int xxx = 0;
            int yyy = 0;

            //判斷是否可以轉換為數字,如果可以就按數字比較
            if (int.TryParse(xx.ToString(), out xxx) && int.TryParse(yy.ToString(), out yyy))
                return xxx.CompareTo(yyy);//按數字比較
            return string.Compare(xx, yy);//按字元比較
        }
    }
}