Go Premium for a chance to win a PS4. Enter to Win


Determine the datatype of the columns to sort in a listview

Posted on 2004-10-31
Medium Priority
Last Modified: 2010-04-23

How can you determine the datatype of the column of a listview so you can sort it properly e.g, if its numeric, string or date.

I am doing a sort on a listview and it always treat the column as string, that's why it doesn't sort properly on numeric values.

Question by:MsFox
LVL 10

Accepted Solution

prakash_prk earned 120 total points
ID: 12463181
Public Class ListViewSorter
    Implements IComparer
    Private mNumeric As Boolean, mDate As Boolean, ClickedColumn As Integer, isAscending As Boolean = True

    Public Sub New(ByRef ListView As System.Windows.Forms.ListView, ByVal ivlClickedColumn As Integer, ByVal bvlisAscending As Boolean)
        ClickedColumn = ivlClickedColumn
        isAscending = bvlisAscending
        mNumeric = False
        mDate = False
        If ListView.Items.Count > 0 Then
            If Microsoft.VisualBasic.IsNumeric(ListView.Items(0).SubItems(ClickedColumn).Text.ToUpper.Trim) Then
                mNumeric = True
            ElseIf Microsoft.VisualBasic.IsDate(ListView.Items(0).SubItems(ClickedColumn).Text.ToUpper.Trim) Then
                mDate = True
            End If
        End If
    End Sub

    Private 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
            If mNumeric Then
                If isAscending = True Then
                    Return CDbl(lv1.SubItems(ClickedColumn).Text) + CDbl(lv2.SubItems(ClickedColumn).Text)
                    Return CDbl(lv1.SubItems(ClickedColumn).Text) - CDbl(lv2.SubItems(ClickedColumn).Text)
                End If
            ElseIf mNumeric = False And mDate = False Then
                If isAscending = True Then
                    Return String.Compare(lv2.SubItems(ClickedColumn).Text, lv1.SubItems(ClickedColumn).Text)
                    Return String.Compare(lv1.SubItems(ClickedColumn).Text, lv2.SubItems(ClickedColumn).Text)
                End If
                If isAscending = True Then
                    Return Date.Compare(CDate(Microsoft.VisualBasic.FormatDateTime(lv2.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)), CDate(Microsoft.VisualBasic.FormatDateTime(lv1.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)))
                    Return Date.Compare(CDate(Microsoft.VisualBasic.FormatDateTime(lv1.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)), CDate(Microsoft.VisualBasic.FormatDateTime(lv2.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)))
                End If
            End If
        Catch ex As Exception
        End Try
    End Function
End Class

Author Comment

ID: 12468060
Thanks.  Exactly what I needed.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question