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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / CSharp Tutorial » GUI Windows Forms » ListView 




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(00);
        this.listView1.Size = new System.Drawing.Size(292266);
        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(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292266);
        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 == itemYreturn 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.