Click here to Skip to main content
11,480,626 members (64,476 online)
Click here to Skip to main content

ListViewSortManager control

, 12 Dec 2004 289.9K 1.5K 97
Rate this:
Please Sign up or sign in to vote.
Add column sorting to the ListView control the easy way.

Introduction

.NET's ListView control is a nice wrapper around the native List control, but the support for column sorting falls a bit short of what's commonly necessary. Some solutions to this problem require you to derive from a class that implements column sorting however. Since inheritance introduces very tight coupling between components, it is not the appropriate solution in every case. For example, your custom ListView derives from a class that implements functionality that you need except for column sorting and you cannot change the base class.

ListViewSortManager provides several useful features:

  • Easy to use.
  • Case-sensitive (ListViewTextSort) and case-insensitive (ListViewTextCaseInsensitiveSort) text sorting.
  • Integer (ListViewInt32Sort and ListViewInt64Sort) and floating-point (ListViewDoubleSort) sorting.
  • Date (ListViewDateSort) sorting.
  • Extendable for other specialized user-provided sort orders.
  • Transparently handles null and empty strings.
  • Transparently handles ascending and descending orderings.
  • Displays the sort order image in the column headers.
  • The user can specify the column and sort order at any time.
  • Automatic sorting can be disabled for batch insertion of elements to the list.

Usage

  1. Add a variable of type ListViewSortManager.
    private ListViewSortManager m_sortMgr;
  2. In your form's constructor, after the call to InitializeComponent(), construct the ListViewSortManager passing the list and an array of Type for sorters. There should be one entry in the array for each column in your list. The constructor takes care of hooking into the ListView's ColumnClick event.
    public MainForm()
    {
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();
    
       m_sortMgr = new ListViewSortManager(m_list, 
          new Type[] {
             typeof(ListViewTextSort),
             typeof(ListViewTextCaseInsensitiveSort),
             typeof(ListViewIntegerSort),
             typeof(ListViewFloatSort),
             typeof(ListViewDateSort)
          }
       );
    }
  3. Voilá! Column sorting has been added to your list.

Extended Usage

If you need to provide a sort ordering that doesn't match the ones provided, you just have to derive a class from ListViewTextSort and, override the OnCompare() method which receives two strings that have to be converted to whatever format that your comparison is based on.

public class ListViewDateSort: ListViewTextSort
{
    public ListViewDateSort(int column, bool ascending):
        base(column, ascending)
    {
    }

    protected override int OnCompare(string lhs, string rhs)
    {
        return DateTime.Parse(lhs).CompareTo(DateTime.Parse(rhs));
    }
}

To Do

  • Investigate alternative, more efficient solutions for sorting that don't require text conversions. Probably, using ListView.Tag and adding overridables to ListViewSortManager.
  • As soon as C# implements generics, implement the comparers as templates.

Updates

Version 1.4 (12/11/04)

  • Added the SortEnabled property to enable/disable automatic sorting of the list's elements. Setting SortEnabled to false before adding a large number of elements to the list will speed up the insertion considerably. (Thanks to Jimmy S. for the suggestion.)
  • Added support for native header sort arrows when using version 6 of the Common Controls library. (Thanks to Arlen Feldman for the suggestion.)

Version 1.3 (02/17/03)

  • ShowHeaderIcon() always left-aligned the column header text regardless of the settings. (Thanks to Jezbo for spotting this).
  • ShowHeaderIcon() now locates the sorting arrow icon on the opposite side of the column header text.

Version 1.2 (11/08/02)

  • The ListViewSortManager's constructor now hooks up to the ColumnClick event making it much easier to use the control. (Thanks to debaser for the suggestion).
  • I forgot to set the transparency for the arrow images. (Thanks to Carlo J. Bos and Cory Smith for spotting this).
  • Made Sort() public again and added an overload that allows to select the column and sort order used. Also added Column and SortOrder properties (Thanks to Cory Smith for the suggestion).

Version 1.1 (11/07/02)

  • Implemented ascending/descending arrow images in column headers. No code changes are necessary to benefit from this.

Version 1.0 (04/22/02)

  • Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Eddie Velasquez
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralA Big Thank You... Pin
Roland Zerek28-Jul-08 11:49
memberRoland Zerek28-Jul-08 11:49 
GeneralMulticolumn Sorting available! [modified] Pin
Luis Alonso Ramos1-Dec-07 11:13
memberLuis Alonso Ramos1-Dec-07 11:13 
GeneralProblems with native arrows Pin
Mockum30-Jul-07 11:35
memberMockum30-Jul-07 11:35 
GeneralRe: Problems with native arrows Pin
Ryan Dunn18-Apr-08 9:02
memberRyan Dunn18-Apr-08 9:02 
QuestionVS2005 bug - arrows don't display Pin
Stoj9-May-07 13:24
memberStoj9-May-07 13:24 
AnswerRe: VS2005 bug - arrows don't display Pin
Amro Khasawneh19-Jun-07 3:44
memberAmro Khasawneh19-Jun-07 3:44 
QuestionProblem when resize column width Pin
whiteshark20032-May-07 22:48
memberwhiteshark20032-May-07 22:48 
GeneralSorting Text with Numbers [modified] Pin
DeeJRoss28-Aug-06 12:02
memberDeeJRoss28-Aug-06 12:02 
GeneralRe: Sorting Text with Numbers Pin
Eddie Velasquez28-Aug-06 12:21
memberEddie Velasquez28-Aug-06 12:21 
QuestionCopyright? Pin
dkintgen2-Aug-06 6:16
memberdkintgen2-Aug-06 6:16 
AnswerRe: Copyright? Pin
Eddie Velasquez2-Aug-06 6:21
memberEddie Velasquez2-Aug-06 6:21 
Generalnulls crash comparer Pin
Capt'N Stabbin'17-Jul-06 19:03
memberCapt'N Stabbin'17-Jul-06 19:03 
GeneralAuto Filter ListView Pin
ANIL KUMAR SHARMA (INDIA)1-Apr-06 0:15
memberANIL KUMAR SHARMA (INDIA)1-Apr-06 0:15 
AnswerRe: Auto Filter ListView Pin
Capt'N Stabbin'17-Jul-06 8:13
memberCapt'N Stabbin'17-Jul-06 8:13 
GeneralRe: Auto Filter ListView [modified] Pin
ANIL KUMAR SHARMA (INDIA)17-Jul-06 19:48
memberANIL KUMAR SHARMA (INDIA)17-Jul-06 19:48 
QuestionListViewDateSort with date format dd-MM-yyyy doesn't work? Pin
id10t30-Mar-06 6:44
memberid10t30-Mar-06 6:44 
AnswerRe: ListViewDateSort with date format dd-MM-yyyy doesn't work? Pin
Paula Garden22-Jul-07 17:53
memberPaula Garden22-Jul-07 17:53 
GeneralGood Solution Pin
norm.net14-Mar-06 23:17
membernorm.net14-Mar-06 23:17 
AnswerRe: Good Solution Pin
Capt'N Stabbin'17-Jul-06 8:18
memberCapt'N Stabbin'17-Jul-06 8:18 
GeneralConversion To Vb.net Pin
eatwork12-Jan-06 7:05
membereatwork12-Jan-06 7:05 
GeneralRe: Conversion To Vb.net Pin
Darshon6-Jun-08 19:37
memberDarshon6-Jun-08 19:37 
GeneralRe: Conversion To Vb.net Pin
Member 40591424-Oct-08 12:34
memberMember 40591424-Oct-08 12:34 
GeneralImageList images overwriting arrows Pin
Milligrant1-Nov-05 8:38
memberMilligrant1-Nov-05 8:38 
GeneralColumn without Comparer Pin
Eugene Pankov6-Sep-05 5:32
memberEugene Pankov6-Sep-05 5:32 
GeneralMinor typo in article Pin
wout de zeeuw28-Aug-05 11:08
memberwout de zeeuw28-Aug-05 11:08 
GeneralForeColor issue Pin
bjorn_i3-Aug-05 12:49
memberbjorn_i3-Aug-05 12:49 
GeneralColumnheader.text disappearing Pin
welberger24-Jul-05 11:51
memberwelberger24-Jul-05 11:51 
QuestionRe: Columnheader.text disappearing Pin
headoo3-Mar-06 7:10
memberheadoo3-Mar-06 7:10 
AnswerRe: Columnheader.text disappearing Pin
headoo6-Mar-06 9:23
memberheadoo6-Mar-06 9:23 
GeneralRe: Columnheader.text disappearing Pin
Rodney Rodríguez21-Jun-06 16:35
memberRodney Rodríguez21-Jun-06 16:35 
GeneralRe: Columnheader.text disappearing Pin
dkintgen23-May-06 8:36
memberdkintgen23-May-06 8:36 
GeneralRe: Columnheader.text disappearing Pin
aldema7-Sep-06 10:36
memberaldema7-Sep-06 10:36 
Generaltransparant property of listview.items.imagelist not working Pin
timbo47523-Jul-05 4:22
membertimbo47523-Jul-05 4:22 
GeneralMultiple Sort Pin
Luis Alonso Ramos21-Jul-05 15:06
memberLuis Alonso Ramos21-Jul-05 15:06 
GeneralRe: Multiple Sort Pin
Eddie Velasquez22-Jul-05 6:07
memberEddie Velasquez22-Jul-05 6:07 
GeneralRe: Multiple Sort Pin
Luis Alonso Ramos22-Jul-05 6:37
memberLuis Alonso Ramos22-Jul-05 6:37 
GeneralRe: Multiple Sort Pin
Eddie Velasquez22-Jul-05 7:13
memberEddie Velasquez22-Jul-05 7:13 
GeneralRe: Multiple Sort Pin
Luis Alonso Ramos22-Jul-05 7:33
memberLuis Alonso Ramos22-Jul-05 7:33 
GeneralRe: Multiple Sort Pin
Capt'N Stabbin'17-Jul-06 5:58
memberCapt'N Stabbin'17-Jul-06 5:58 
GeneralRe: Multiple Sort Pin
Luis Alonso Ramos17-Jul-06 7:30
memberLuis Alonso Ramos17-Jul-06 7:30 
QuestionWhat about VS2005? Pin
TheChemist3513-Jun-05 12:11
memberTheChemist3513-Jun-05 12:11 
AnswerRe: What about VS2005? Pin
Bo Skjoett21-Apr-06 0:35
memberBo Skjoett21-Apr-06 0:35 
GeneralRe: What about VS2005? Pin
Stoj9-May-07 13:23
memberStoj9-May-07 13:23 
GeneralRe: What about VS2005? Pin
Bo Skjoett9-May-07 20:55
memberBo Skjoett9-May-07 20:55 
GeneralVery Nice! Pin
RFID Chris9-Jun-05 11:11
memberRFID Chris9-Jun-05 11:11 
GeneralRe: Very Nice! Pin
Eddie Velasquez12-Jun-05 8:55
memberEddie Velasquez12-Jun-05 8:55 
GeneralCLS Compliant Pin
Philip Lee6-Jun-05 22:39
memberPhilip Lee6-Jun-05 22:39 
GeneralRe: CLS Compliant Pin
Eddie Velasquez12-Jun-05 9:00
memberEddie Velasquez12-Jun-05 9:00 
GeneralVisual Style problem Pin
DarthVeda31-May-05 2:09
memberDarthVeda31-May-05 2:09 
GeneralRe: Visual Style problem Pin
id10t1-Jun-05 10:53
memberid10t1-Jun-05 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Dec 2004
Article Copyright 2002 by Eddie Velasquez
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid