ListView Sorter : ListView « GUI Windows Forms « C# / CSharp Tutorial






ListView Sorter
using System;
using System.Collections;
using System.Windows.Forms;

class FolderHolder: Form
{
    public FolderHolder()
    {
        InitializeComponent();
    }

    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        ListViewItemComparer sorter = listView1.ListViewItemSorter as ListViewItemComparer;

        if (sorter == null)
        {
            sorter = new ListViewItemComparer(e.Column);
            listView1.ListViewItemSorter = sorter;
        }
        else
        {
            sorter.Column = e.Column;
        }

        listView1.Sort();
    }

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new FolderHolder());
    }

    private void InitializeComponent()
    {
        System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"1","B","A"}, -1);
        System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {"2","A","D"}, -1);
        System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {"4","C","B"}, -1);
        this.listView1 = new System.Windows.Forms.ListView();
        this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
        this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
        this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
        this.SuspendLayout();
        // 
        // listView1
        // 
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {this.columnHeader1,this.columnHeader2,this.columnHeader3});
        this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView1.GridLines = true;
        this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {listViewItem1,listViewItem2,listViewItem3});
        this.listView1.Location = new System.Drawing.Point(0, 0);
        this.listView1.Size = new System.Drawing.Size(292, 266);
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
        // 
        // FolderHolder
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.listView1);
        this.ResumeLayout(false);

    }

    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ColumnHeader columnHeader1;
    private System.Windows.Forms.ColumnHeader columnHeader2;
    private System.Windows.Forms.ColumnHeader columnHeader3;
}

public class ListViewItemComparer : IComparer
{
    private int column;
    private bool numeric = false;

    public int Column
    {
        get { return column; }
        set { column = value; }
    }

    public bool Numeric
    {
        get { return numeric; }
        set { numeric = value; }
    }

    public ListViewItemComparer(int columnIndex)
    {
        Column = columnIndex;
    }

    public int Compare(object x, object y)
    {
        ListViewItem itemX = x as ListViewItem;
        ListViewItem itemY = y as ListViewItem;

        if (itemX == null && itemY == null) 
           return 0;
        else if (itemX == null) 
           return -1;
        else if (itemY == null) 
           return 1;

        if (itemX == itemY) return 0;

        if (Numeric)
        {
            decimal itemXVal, itemYVal;

            if (!Decimal.TryParse(itemX.SubItems[Column].Text, out itemXVal))
            {
                itemXVal = 0;
            }
            if (!Decimal.TryParse(itemY.SubItems[Column].Text, out itemYVal))
            {
                itemYVal = 0;
            }

            return Decimal.Compare(itemXVal, itemYVal);
        } else {
            string itemXText = itemX.SubItems[Column].Text;
            string itemYText = itemY.SubItems[Column].Text;

            return String.Compare(itemXText, itemYText);
        }
    }
}








23.29.ListView
23.29.1.ListView SorterListView Sorter
23.29.2.Fill data from Database to ListView
23.29.3.ListView Drives
23.29.4.ListView for File and Folder