Question An interesting challenge? listview sort

imsiyat

New member
Joined
Jul 25, 2011
Messages
3
Programming Experience
3-5
Hi friends. listview in my hand I want to sort by date column and I found this code. but I have a problem like this: I can not do it a second time!

class in comparison:

VB.NET:
Public Class ListViewColumnSorter
    Implements System.Collections.IComparer
    Private ColumnToSort As Integer
    Private OrderOfSort As SortOrder
    Private ObjectCompare As CaseInsensitiveComparer
    Private TypeOfSort As enumSortType

    Enum enumSortType
        AlphaSort
        NumericSort
        DateSort
    End Enum

    Public Sub New()
        ' Initialize the column to '0'.
        ColumnToSort = 3

        ' Initialize the sort order to 'none'.
        OrderOfSort = SortOrder.Ascending

        ' Initialize the CaseInsensitiveComparer object.
        ObjectCompare = New CaseInsensitiveComparer

        ' Initialize the default column type to be Alpha.
        TypeOfSort = enumSortType.DateSort
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
[COLOR=#ff0000][B]'i dont want coming to here line on the insertrecord sub[/B]
[/COLOR]        Dim compareResult As Integer
        Dim listviewX As ListViewItem
        Dim listviewY As ListViewItem

        ' Cast the objects to be compared to ListViewItem objects.
        listviewX = CType(x, ListViewItem)
        listviewY = CType(y, ListViewItem)

        ' Compare the two items.
        Select Case TypeOfSort
            Case enumSortType.AlphaSort
                compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)

            Case enumSortType.NumericSort
                Dim intx As Integer = CInt(listviewX.SubItems(ColumnToSort).Text)
                Dim inty As Integer = CInt(listviewY.SubItems(ColumnToSort).Text)

                If intx = inty Then
                    compareResult = 0
                ElseIf intx > inty Then
                    compareResult = 1
                Else
                    compareResult = -1
                End If
            Case enumSortType.DateSort

                Dim intx As DateTime = CDate(listviewX.SubItems(ColumnToSort).Text)
                Dim inty As DateTime = CDate(listviewY.SubItems(ColumnToSort).Text)

                If intx = inty Then
                    compareResult = 0
                ElseIf intx > inty Then
                    compareResult = 1
                Else
                    compareResult = -1
                End If

        End Select


        ' Calculate the correct return value based on the object
        ' comparison.
        If (OrderOfSort = SortOrder.Ascending) Then
            ' Ascending sort is selected, return typical result of
            ' compare operation.
            Return compareResult
        ElseIf (OrderOfSort = SortOrder.Descending) Then
            ' Descending sort is selected, return negative result of
            ' compare operation.
            Return (-compareResult)
        Else
            ' Return '0' to indicate that they are equal.
            Return 0
        End If
    End Function

    Public Property SortColumn() As Integer
        Set(ByVal Value As Integer)
            ColumnToSort = Value
        End Set

        Get
            Return ColumnToSort
        End Get
    End Property

    Public Property Order() As SortOrder
        Set(ByVal Value As SortOrder)
            OrderOfSort = Value
        End Set

        Get
            Return OrderOfSort
        End Get
    End Property

    Public Property SortType() As enumSortType
        Get
            Return TypeOfSort
        End Get
        Set(ByVal Value As enumSortType)
            TypeOfSort = Value
        End Set
    End Property
End Class

inserting record sub:

VB.NET:
Sub insertrecord()
        Dim sozcuk As OleDb.OleDbDataReader
        Dim sozcuk1 As OleDb.OleDbDataReader
        Dim item As New ListViewItem()
        Try
            baglanti = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=xxx.mdb;Jet OLEDB:Database Password=xxx;")
            baglanti.Open()
            Dim command As New OleDb.OleDbCommand
            Dim command1 As New OleDb.OleDbCommand
            command = baglanti.CreateCommand
            command1 = baglanti.CreateCommand
            command1.CommandText = "select * from ilerikayit"
            sozcuk1 = command1.ExecuteReader()
            If sozcuk1.HasRows Then
                While sozcuk1.Read
                    'MessageBox.Show(sozcuk1("aboneno".ToString))
                    command.CommandText = "select * from xxx where aboneno= " & sozcuk1("aboneno") & ""
                    sozcuk = command.ExecuteReader()
                    If sozcuk.HasRows Then
                        While sozcuk.Read
                            item = ListView2.Items.Add(sozcuk("aboneno".ToString))
[COLOR=#ff0000][B]                            'jumping the comparison class this line before second record and i get error
[/B][/COLOR]                            With item
                                .SubItems.Add(sozcuk("ad".ToString))
                                .SubItems.Add(sozcuk("soyad".ToString))
                                .SubItems.Add(sozcuk1("Tarih".ToString))
                                .SubItems.Add(sozcuk1("verilensaat".ToString))
                                .SubItems.Add(sozcuk1("Tur".ToString))
                            End With
                        End While
                    End If
                    sozcuk.Close()
                    '   MessageBox.Show(sozcuk("ad".ToString))
                End While
                sozcuk1.Close()
            End If

        Catch ex As Exception
            MsgBox("Hata Oluştu:" & ex.Message)
        End Try
        baglanti.Close()
    End Sub

and i do this like that:

insertrecord()
lvwColumnSorter1 = New ListViewColumnSorter
Me.ListView2.ListViewItemSorter = lvwColumnSorter1


sorry may bad english :) . please help!
 
You get a ArgumentOutOfRange exception, due to attempting to access a subitem index that does not exist, because you added a new item with no subitems. Instead create the complete ListViewItem first, then add it to the ListView.Items.

It is not necessary to create and assign a new ListViewColumnSorter object, the ListView already has this and automatically sort when a new item is added.
When if a different sort is needed the same applies, just change the existing ListViewColumnSorter object and call Listview.Sort to apply the new sort order.

.SubItems.Add(sozcuk("ad".ToString))
Just a comment about this code, especially look at this part: "ad".ToString - why convert a string to string?? Surely it is the data returned by datareader item that needs to be converted to string?
 
yesss it's possible. i try quickly. thank you :eek:

and you right. but i dont know why convert string to string. possible i missing. thank you again :)
 
thanks!

its working with this method:

Dim str(5) As String
Dim itm As ListViewItem
str(0) = "Rob Machy"
str(1) = "100 North Ave"
str(2) = "Business Manager"
str(3) = "89,000"
str(4) = "Development"
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
 
Back
Top