Click here to Skip to main content
13,147,050 members (64,139 online)
Click here to Skip to main content

Stats

52.7K views
1K downloads
52 bookmarked
Posted 12 Oct 2006

An extendable ListViewSorter

, 12 Oct 2006
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)

Share

About the Author

Marcus Deecke
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 12 Oct 2006
Article Copyright 2006 by Marcus Deecke
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid