Important alert: (current site time 7/15/2013 8:40:33 AM EDT)
 

VB icon

_An alternate list view sort for Vb.Net

Email
Submitted on: 10/10/2003 8:23:07 AM
By: Duane Warsham 
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB.NET
Views: 21541
(About the author)
 
     To provide an alternative to the Microsoft recommended sorting in the list view control.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: _An alternate list view sort for Vb.Net
// Description:To provide an alternative to the Microsoft recommended sorting in the list view control.
// By: Duane Warsham
//
// Inputs:ListView object, column number, and type of sort (date, string, integer)
//
// Returns:returns sorted list view
//
// Side Effects:The ListView Sorting property needs to be set to None for this sub to work correctly.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1634&lngWId=10//for details.//**************************************

Public Sub ListViewSort(ByRef lv As ListView, ByVal col As Integer, ByVal compareType As VariantType)
 'This will sort a list view by any passed column number, you will pass the listview object, the
 'column number, and the type of compare you want in the variant type, if you need more than the 
 '3 comparisons belowm just copy the sort code, add to the case statement, and change the Cxxx function
 'to use the correct one of choice.
 Dim lvTempItem As ListViewItem
 Dim i As Integer
 Select Case compareType
'sort by a date column
Case VariantType.Date
For i = 0 To lv.Items.Count - 2 'only need to check up to the next to last in list
 'compare current item to the next one
 If CDate(lv.Items(i).SubItems(col).Text) > CDate(lv.Items(i + 1).SubItems(col).Text) Then
 'save the current item
 lvTempItem = lv.Items(i)
 'remove the current item, has to be removed from collection before reinserting it
 lv.Items(i).Remove()
 'now add the item back in at its new location
 lv.Items.Insert((i + 1), lvTempItem)
 'start at first again to find next item needing moving
 i = -1
 End If
Next i
' sort by a whole number
Case VariantType.Integer
For i = 0 To lv.Items.Count - 2 'only need to check up to the next to last in list
 'compare current item to the next one
 If CInt(lv.Items(i).SubItems(col).Text) > CInt(lv.Items(i + 1).SubItems(col).Text) Then
 'save the current item
 lvTempItem = lv.Items(i)
 'remove the current item, has to be removed from collection before reinserting it
 lv.Items(i).Remove()
 'now add the item back in at its new location
 lv.Items.Insert((i + 1), lvTempItem)
 'start at first again to find next item needing moving
 i = -1
 End If
Next i
'string sort
Case VariantType.String
For i = 0 To lv.Items.Count - 2 'only need to check up to the next to last in list
 'compare current item to the next one
 If CStr(lv.Items(i).SubItems(col).Text) > CStr(lv.Items(i + 1).SubItems(col).Text) Then
 'save the current item
 lvTempItem = lv.Items(i)
 'remove the current item, has to be removed from collection before reinserting it
 lv.Items(i).Remove()
 'now add the item back in at its new location
 lv.Items.Insert((i + 1), lvTempItem)
 'start at first again to find next item needing moving
 i = -1
 End If
Next i
 End Select
 'if you are calling this sort sub from another class other than where this sub resides, you will need to 
 'put the refresh in the calling sub after the sort call, otherwise you can uncomment the refresh, and
 'leave it here.
 'Me.lvEvents.Refresh()
 End Sub


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
12/2/2003 8:51:03 PM

I was pulling my hair out over sorting listviews - it was sooo easy in vb6! I tried the Microsoft knowledgebase and found article 319399, but I could not get it to work for the life of me. Well done - your code works perfectly and has saved me hours of work.
(If this comment was disrespectful, please report it.)

 
4/18/2006 1:50:35 AMAnwer Khan

Excelent more then Excelent
(If this comment was disrespectful, please report it.)

 
10/5/2006 4:59:42 AMIndi

A lovely piece of lateral thinking.
It saved me a great deal of pain. Thank you.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.