Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will try to sort a ListView.

Code is another common sort, and SubItem sort.
But it is have a problem.
* lvProcess is a ListView / frmMain is a Main Form

VB
Private Sub lvProcess_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvProcess.ColumnClick
       If e.Column = 0 Then
           lvProcess.ListViewItemSorter = New ListViewItemComparer(e.Column)
           '   If lvProcess.Sorting = SortOrder.Ascending Then
           ' lvProcess.Sorting = SortOrder.Descending
           'Else
           'lvProcess.Sorting = SortOrder.Ascending
           'End If
       ElseIf e.Column = 1 Then
       lvProcess.ListViewItemSorter = New ListViewItemComparer(e.Column)
       End If
   End Sub

Public Class ListViewItemComparer
    Implements IComparer

    Private col As Integer

    Public Sub New()
        col = 0
    End Sub

    Public Sub New(ByVal column As Integer)
        col = column
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

        If frmMain.SizeSort = 0 Then
            If CType(x, ListViewItem).SubItems(col).Text.Length = CType(y, ListViewItem).SubItems(col).Text.Length Then
                Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
            Else
                Return CType(x, ListViewItem).SubItems(col).Text.Length.CompareTo(CType(y, ListViewItem).SubItems(col).Text.Length)
            End If
            frmMain.SizeSort = 1
            Exit Function
        Else 'If SizeSort = 0 Then

            If frmMain.lvProcess.Sorting = SortOrder.Ascending Then
                frmMain.lvProcess.Sorting = SortOrder.Descending
            Else
                frmMain.lvProcess.Sorting = SortOrder.Ascending
            End If

    End Function

End Class

The source is working.

But,If I run lvProcess.ListViewItemSorter = New ListViewItemComparer(e.Column)
and
VB
If frmMain.lvProcess.Sorting = SortOrder.Ascending Then
               frmMain.lvProcess.Sorting = SortOrder.Descending
           Else
               frmMain.lvProcess.Sorting = SortOrder.Ascending
           End If

an Dosen't work.

If frmMain.lvProcess.Sorting = SortOrder.Ascending ...
This part doesn't work.

How Can I ?
Posted
Updated 29-Apr-10 2:33am
v3

C#?

== Operator, it should not work in VB.NET not it?
 
Share this answer
 
In the mid term, read a C# book. In the short term, == is the comparison operator, not =.

Note TR: The OP is using VB.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900