Click here to Skip to main content
11,806,026 members (60,453 online)
Click here to Skip to main content

Sortable ListView

, 2 Apr 2010 CPOL 20.1K 13
Rate this:
Please Sign up or sign in to vote.
The Windows Forms ListView control doesn't provide column sorting functionality. This article shows how to implement sorting while taking field type into consideration (sort by date, number, ...)

The Windows Forms ListView control doesn't provide column sorting functionality. So if you click on a column in a ListView Details view, don't expect the items to be sorted by the clicked column. To get this functionality, we'll need to sort the items by the clicked column in the ListView ColumnClick event. I searched online for “Sortable ListView” and I found three MSDN articles talking about this: Sort ListView Column in Visual C#, Sorting ListView Items by Column Using Windows Forms, and How to: Sort ListView Items. None of those implementations takes into consideration the type of the column being sorted. That is, they all do string sorting. If you have dates and numbers in your list, then they’ll not be sorted properly. For example, number 2 will be considered greater than 11. Date time 9/9/1400 will be considered greater than 11/11/2020. Below is an implementation that takes into consideration string, DateTime, int and double types. It can be easily extended to handle more types.

  • Add the SortableListView control to your Windows Form.
  • When adding columns to the SortableListView, set the Tag attribute to the type of the column.
    sortableListView.Columns.Add("String Field").Tag = typeof(string);
    sortableListView.Columns.Add("DateTime Field").Tag = typeof(DateTime);
    sortableListView.Columns.Add("Int Field").Tag = typeof(int);
    sortableListView.Columns.Add("Double Field").Tag = typeof(double);
  • Now, you can add the items as usual.

For example, the below list is sorted by the DateTime field.

Sortable ListView
Sortable ListView

Click on the above image to download the SortableListView control.

Posted in .NET, C#, Uncategorized Tagged: ListView, sort, Sortable, SortableListView, SortByColumn, SortDate, SortDouble, SortInt



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


About the Author

Ali BaderEddin
Software Developer Microsoft
United States United States

You may also be interested in...

Comments and Discussions

GeneralThanks! Pin
Ant210015-Feb-10 23:53
memberAnt210015-Feb-10 23:53 
GeneralRe: Thanks! Pin
Ali BaderEddin16-Feb-10 7:51
memberAli BaderEddin16-Feb-10 7:51 
GeneralRe: Thanks! Pin
GautamBSharma31-May-10 23:56
memberGautamBSharma31-May-10 23:56 
GeneralRe: Thanks! Pin
Ali BaderEddin2-Jun-10 9:12
memberAli BaderEddin2-Jun-10 9:12 

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.151002.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by Ali BaderEddin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid