Click here to Skip to main content
13,201,940 members (74,960 online)
Rate this:
 
Please Sign up or sign in to vote.
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

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
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 28-Apr-10 20:13pm
rus204716
Updated 29-Apr-10 2:33am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

C#?

== Operator, it should not work in VB.NET not it?
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

In the mid term, read a C# book. In the short term, == is the comparison operator, not =.

Note TR: The OP is using VB.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 29 Apr 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100