Mick Dugan Posted October 2, 2004 Posted October 2, 2004 I know I can use the ascending and descending properties to order the first column in a list view, but how can I order the other columns as well? Quote
*Experts* DiverDan Posted October 5, 2004 *Experts* Posted October 5, 2004 Here's one that I've used for a while that seems to work fairly well for both alpha and numeric values. In the ListViews ColumnClick event: Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick Dim bAltSort As Boolean = False Dim MySortClass As New SortClass() Try MySortClass.column = e.Column 'Find out of we are sorting a numeric column If IsNumeric(ListView1.Items(0).SubItems(e.Column).Text.ToUpper.Trim) Then MySortClass.mNumeric = True Else MySortClass.mNumeric = False End If 'Find out if the user is clicking on the same column. If mIntSelectedCol = e.Column Then MySortClass.AltSort = True mIntSelectedCol = -1 Else mIntSelectedCol = e.Column End If ListView1.ListViewItemSorter = MySortClass Catch MessageBox.Show(Err.Description, "Sorting Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub then the Sorter Class Private mIntSelectedCol As Integer Public Class SortClass Implements IComparer Private mColumn As Integer Public mNumeric As Boolean Private m_bAltSort As Boolean Public Property column() As Integer Get Return mColumn End Get Set(ByVal Value As Integer) mColumn = Value End Set End Property Public Property Numeric() As Boolean Get Return mNumeric End Get Set(ByVal Value As Boolean) mNumeric = Value End Set End Property Public Property AltSort() As Boolean Get Return m_bAltSort End Get Set(ByVal Value As Boolean) m_bAltSort = Value End Set End Property Public Function Compare(ByVal x As Object, ByVal y As Object) _ As Integer Implements System.Collections.IComparer.Compare Dim lv1 As ListViewItem = x Dim lv2 As ListViewItem = y Try If mNumeric Then If m_bAltSort = True Then Return CLng(lv1.SubItems(mColumn).Text) + CDbl(lv2.SubItems(mColumn).Text) Else Return CDbl(lv2.SubItems(mColumn).Text) - CDbl(lv1.SubItems(mColumn).Text) End If Else If m_bAltSort = True Then Return String.Compare(lv2.SubItems(mColumn).Text, lv1.SubItems(mColumn).Text) Else Return String.Compare(lv1.SubItems(mColumn).Text, lv2.SubItems(mColumn).Text) End If End If Catch End Try End Function End Class I think that this should work for you. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
kejpa Posted October 5, 2004 Posted October 5, 2004 Hey DiverDan I just want to point out that you're having CLng along with CDbl when doing AltSort on numeric columns. /Kejpa Quote
*Experts* DiverDan Posted October 5, 2004 *Experts* Posted October 5, 2004 Good point! Should be convert to double CDbl instead of convert to Long CLng. Thanks, Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Mick Dugan Posted October 6, 2004 Author Posted October 6, 2004 Thanks! Thanks for the feedback Dan! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.