Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#

An extendable ListViewSorter

Rate me:
Please Sign up or sign in to vote.
4.65/5 (13 votes)
12 Oct 2006CPOL5 min read 63K   1.1K   52  
This article describes how to sort a ListView ascending/descending with two lines of code, and how to extend its sorting capabilities.
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Yaowi.Common.Collections;
using Yaowi.Common.Windows.Controls;

namespace Demo
{
  public partial class Form1 : Form
  {
    // A ListViewSorter member
    ListViewSorter listviewsorter = new ListViewSorter();

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      InitColumns();
      FillListView();

      // Set the ListViewSorters ListView property
      listviewsorter.ListView = this.listView1;

      // Register Comparer for each column
      //listviewsorter.ColumnComparerCollection["String"] = new Yaowi.Common.Collections.StringComparer();
      listviewsorter.ColumnComparerCollection["Int"] = new NumericComparer();
      listviewsorter.ColumnComparerCollection["Decimal"] = new NumericComparer();
      listviewsorter.ColumnComparerCollection["Date"] = new DateComparer();
      listviewsorter.ColumnComparerCollection["Postal Code 1"] = new GermanPostalCodeCityByCityComparer();
      listviewsorter.ColumnComparerCollection["Postal Code 2"] = new GermanPostalCodeCityByPostalCodeComparer();
    }

    /// <summary>
    /// Initializes the columns.
    /// </summary>
    protected void InitColumns()
    {
      this.listView1.Columns.Clear();

      ColumnHeader ch = null;

      ch = new ColumnHeader();
      ch.Text = "String";
      ch.Name = ch.Text;
      ch.Width = 75;
      this.listView1.Columns.Add(ch);

      ch = new ColumnHeader();
      ch.Text = "Int";
      ch.Name = ch.Text;
      ch.Width = 65;
      ch.TextAlign = HorizontalAlignment.Right;
      this.listView1.Columns.Add(ch);

      ch = new ColumnHeader();
      ch.Text = "Decimal";
      ch.Name = ch.Text;
      ch.Width = 120;
      ch.TextAlign = HorizontalAlignment.Right;
      this.listView1.Columns.Add(ch);

      ch = new ColumnHeader();
      ch.Text = "Date";
      ch.Name = ch.Text;
      ch.Width = 120;
      this.listView1.Columns.Add(ch);

      ch = new ColumnHeader();
      ch.Text = "Postal Code 1";
      ch.Name = ch.Text;
      ch.Width = 120;
      this.listView1.Columns.Add(ch);

      ch = new ColumnHeader();
      ch.Text = "Postal Code 2";
      ch.Name = ch.Text;
      ch.Width = 120;
      this.listView1.Columns.Add(ch);
    }

    /// <summary>
    /// Fills the ListView with sample data.
    /// </summary>
    protected void FillListView()
    {
      int cnt = 10;

      String[] postals = new string[cnt];
      postals[0] = "22307 Hamburg";
      postals[1] = "80711 Muenchen";
      postals[2] = "44227 Dortmund";
      postals[3] = "10234 Berlin";
      postals[4] = null;
      postals[5] = "22648 Norderstedt";
      postals[6] = "21026 Hamburg";
      postals[7] = "01465 Dresden";
      postals[8] = "91052 Erlangen";
      postals[9] = "81829 Muenchen";

      Random rd = new Random();

      for (int i = 0; i < cnt; i++)
      {
        ListViewItem item = this.listView1.Items.Add("Item " + i);
        item.SubItems.Add("" + (int)Math.Pow((int)(10 - i + 1), 2));
        item.SubItems.Add("" + new Decimal(((double)rd.Next(1000, 1999)) / 1500));

        // Provide some null values
        if (i == 3 || i == 7)
        {
          String s = null;
          item.SubItems.Add(s);
        }
        else
          item.SubItems.Add("" + (DateTime.Now.AddYears(i).AddMonths((i * 2)).AddHours((i * 10 * -1))));

        item.SubItems.Add(postals[i]);
        item.SubItems.Add(postals[i]);
      }
    }
  }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
I am working for a small software company in Hamburg, Germany, and my day-to-day distress is Java development.

Comments and Discussions