'以下排序代码测试没有问题的。现在想通过调用这个Listview的ColumnClick事件来对Listveiw控件进行排序。请大家帮忙。Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As LongPrivate Declare Function GetTickCount Lib "kernel32" () As LongSub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) Dim lngStart As Long
lngStart = GetTickCount With ListView1 Dim lngCursor As Long
lngCursor = .MousePointer
.MousePointer = vbHourglass LockWindowUpdate .hWnd Dim l As Long
Dim strFormat As String
Dim strData() As String Dim lngIndex As Long
lngIndex = ColumnHeader.Index - 1 Select Case UCase$(ColumnHeader.Tag)'排序日期==========================================
Case "DATE" strFormat = "YYYYMMDDHhNnSs" With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l
Else
For l = 1 To .Count
With .Item(l)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l
End If
End With
' .SortOrder = (.SortOrder + 1) Mod 2 '是否升序或者降序
.SortKey = ColumnHeader.Index - 1
.Sorted = True With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l
Else
For l = 1 To .Count
With .Item(l)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l
End If
End With End Select LockWindowUpdate 0& .MousePointer = lngCursor End With
'=======================================================
(ByVal hWndLock As Long) As LongPrivate Declare Function GetTickCount Lib "kernel32" () As LongSub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) Dim lngStart As Long
lngStart = GetTickCount With ListView1 Dim lngCursor As Long
lngCursor = .MousePointer
.MousePointer = vbHourglass LockWindowUpdate .hWnd Dim l As Long
Dim strFormat As String
Dim strData() As String Dim lngIndex As Long
lngIndex = ColumnHeader.Index - 1 Select Case UCase$(ColumnHeader.Tag)'排序日期==========================================
Case "DATE" strFormat = "YYYYMMDDHhNnSs" With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l
Else
For l = 1 To .Count
With .Item(l)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l
End If
End With
' .SortOrder = (.SortOrder + 1) Mod 2 '是否升序或者降序
.SortKey = ColumnHeader.Index - 1
.Sorted = True With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l
Else
For l = 1 To .Count
With .Item(l)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l
End If
End With End Select LockWindowUpdate 0& .MousePointer = lngCursor End With
'=======================================================
如果需要按数字、日期、IP地址等排序,需要用到API。
其中的1就是对应第几列