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:
inserting record sub:
and i do this like that:
insertrecord()
lvwColumnSorter1 = New ListViewColumnSorter
Me.ListView2.ListViewItemSorter = lvwColumnSorter1
sorry may bad english
. please help!
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