Skip to main content
Email Password   helpLost your password?

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:

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

Updates

Version 1.4 (12/11/04)

Version 1.3 (02/17/03)

Version 1.2 (11/08/02)

Version 1.1 (11/07/02)

Version 1.0 (04/22/02)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralA Big Thank You... Pin
Roland Zerek
11:49 28 Jul '08  
GeneralMulticolumn Sorting available! [modified] Pin
Luis Alonso Ramos
11:13 1 Dec '07  
GeneralProblems with native arrows Pin
Mockum
11:35 30 Jul '07  
GeneralRe: Problems with native arrows Pin
Ryan Dunn
9:02 18 Apr '08  
QuestionVS2005 bug - arrows don't display Pin
Stoj
13:24 9 May '07  
AnswerRe: VS2005 bug - arrows don't display Pin
Amro Khasawneh
3:44 19 Jun '07  
QuestionProblem when resize column width Pin
whiteshark2003
22:48 2 May '07  
GeneralSorting Text with Numbers [modified] Pin
DeeJRoss
12:02 28 Aug '06  
GeneralRe: Sorting Text with Numbers Pin
Eddie Velasquez
12:21 28 Aug '06  
QuestionCopyright? Pin
dkintgen
6:16 2 Aug '06  
AnswerRe: Copyright? Pin
Eddie Velasquez
6:21 2 Aug '06  
Generalnulls crash comparer Pin
Capt'N Stabbin'
19:03 17 Jul '06  
GeneralAuto Filter ListView Pin
ANIL KUMAR SHARMA (INDIA)
0:15 1 Apr '06  
AnswerRe: Auto Filter ListView Pin
Capt'N Stabbin'
8:13 17 Jul '06  
GeneralRe: Auto Filter ListView [modified] Pin
ANIL KUMAR SHARMA (INDIA)
19:48 17 Jul '06  
GeneralListViewDateSort with date format dd-MM-yyyy doesn't work? Pin
id10t
6:44 30 Mar '06  
GeneralRe: ListViewDateSort with date format dd-MM-yyyy doesn't work? Pin
Paula Garden
17:53 22 Jul '07  
GeneralGood Solution Pin
norm.net
23:17 14 Mar '06  
AnswerRe: Good Solution Pin
Capt'N Stabbin'
8:18 17 Jul '06  
GeneralConversion To Vb.net Pin
eatwork
7:05 12 Jan '06  
GeneralRe: Conversion To Vb.net Pin
Darshon
19:37 6 Jun '08  
GeneralRe: Conversion To Vb.net Pin
Member 405914
12:34 24 Oct '08  
GeneralImageList images overwriting arrows Pin
Milligrant
8:38 1 Nov '05  
GeneralColumn without Comparer Pin
Eugene Pankov
5:32 6 Sep '05  
GeneralMinor typo in article Pin
wout de zeeuw
11:08 28 Aug '05  


Last Updated 12 Dec 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009