Solved

Determine the datatype of the columns to sort in a listview

Posted on 2004-10-31
164 Views
Last Modified: 2010-04-23
Hi,

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.

Thanks.
0
Question by:MsFox
    2 Comments
     
    LVL 10

    Accepted Solution

    by:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistviewclasslistviewitemsortertopic.asp
    --------------------------------------------
    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
            Try
                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)
                    Else
                        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)
                    Else
                        Return String.Compare(lv1.SubItems(ClickedColumn).Text, lv2.SubItems(ClickedColumn).Text)
                    End If
                Else
                    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)))
                    Else
                        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
                msgbox(ex.tostring)
            End Try
        End Function
    End Class
    ------------------
    0
     
    LVL 1

    Author Comment

    by:MsFox
    Thanks.  Exactly what I needed.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    860 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now