"sonu" <so*************@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have a problem regarding use of a ListviewSorter class which is used
to sort the items in the
listview.
Here is the code I use.
Enum PCOColumns
PersonID
Result
Type
EffectiveDate
SubmitDate
End Enum
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
....
' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter2 = New ListViewColumnSorter
Me.lvwPCOs.ListViewItemSorter = lvwColumnSorter2
....
End Sub
Private Sub lvwPCOs_ColumnClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lvwPCOs.ColumnClick
Select Case e.Column
Case PCOColumns.PersonID
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.NumericSort
Case PCOColumns.SubmitDate, PCOColumns.EffectiveDate
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.DateSort
Case Else
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.AlphaSort
End Select
' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter2.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorter2.Order = SortOrder.Ascending) Then
lvwColumnSorter2.Order = SortOrder.Descending
Else
lvwColumnSorter2.Order = SortOrder.Ascending
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter2.SortColumn = e.Column
lvwColumnSorter2.Order = SortOrder.Ascending
End If
' Perform the sort with these new sort options.
Me.lvwPCOs.Sort()
End Sub
ListViewColumnSorter.vb:
Imports System.Collections
Imports System.Windows.Forms
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 = 0
' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None
' Initialize the CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer
' Initialize the default column type to be Alpha.
TypeOfSort = enumSortType.AlphaSort
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
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(ColumnToS ort).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 datex As Date =
Convert.ToDateTime(listviewX.SubItems(ColumnToSort ).Text)
Dim datey As Date =
Convert.ToDateTime(listviewY.SubItems(ColumnToSort ).Text)
If datex = datey Then
compareResult = 0
ElseIf datex > datey 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
Greg