关于LISTVIEW的排序问题!

gebobo 2002-12-30 11:10:21
ListView1.Sorted = True '将列表排序
ListView1.SortOrder = 1 '''降序
ListView1.SortKey = ColumnHeader.Index - 1
怎么对数字型的不能正常的排序,谢谢!
...全文
11 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttyp 2002-12-30
对于楼上的说法,当数字为负数时是不能奏效的~~~
回复
fling_boy 2002-12-30
在listview里所有数据都表示成了字符串,所以比较时也是以字符串方式进行。
想要达到你的效果,最好格式化一下。
例如:
1
2
3
6
71
345
-----------------
001
002
003
006
071
345
------------------
这样就OK.
回复
ttyp 2002-12-30
Option Explicit
Public objFind As LV_FINDINFO
Public objItem As LV_ITEM
Public sOrder As Boolean

Public Type POINTAPI
x As Long
y As Long
End Type

Public Type LV_FINDINFO
flags As Long
psz As String
lParam As Long
pt As POINTAPI
vkDirection As Long
End Type

Public Type LV_ITEM
mask As Long
iItem As Long
iSubItem As Long
State As Long
stateMask As Long
pszText As String
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long
End Type

Public Const LVFI_PARAM As Long = &H1
Public Const LVIF_TEXT As Long = &H1

Public Const LVM_FIRST As Long = &H1000
Public Const LVM_FINDITEM As Long = (LVM_FIRST + 13)
Public Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Public Const LVM_SORTITEMS As Long = (LVM_FIRST + 48)

Public m_Col As Integer

'API declarations
Public Declare Function SendMessage Lib "USER32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Public Function CompareDates(ByVal lParam1 As Long, _
ByVal lParam2 As Long, _
ByVal hwnd As Long) As Long

Dim dDate1 As Date
Dim dDate2 As Date

dDate1 = ListView_GetItemDate(hwnd, lParam1)
dDate2 = ListView_GetItemDate(hwnd, lParam2)

Select Case sOrder
Case True: 'sort descending

If dDate1 < dDate2 Then
CompareDates = 0
ElseIf dDate1 = dDate2 Then
CompareDates = 1
Else: CompareDates = 2
End If

Case Else: 'sort ascending

If dDate1 > dDate2 Then
CompareDates = 0
ElseIf dDate1 = dDate2 Then
CompareDates = 1
Else: CompareDates = 2
End If

End Select

End Function


Public Function CompareValues(ByVal lParam1 As Long, _
ByVal lParam2 As Long, _
ByVal hwnd As Long) As Long


Dim val1 As Double
Dim val2 As Double

val1 = ListView_GetItemValueStr(hwnd, lParam1)
val2 = ListView_GetItemValueStr(hwnd, lParam2)

Select Case sOrder
Case True: 'sort descending

If val1 < val2 Then
CompareValues = 0
ElseIf val1 = val2 Then
CompareValues = 1
Else: CompareValues = 2
End If

Case Else: 'sort ascending

If val1 > val2 Then
CompareValues = 0
ElseIf val1 = val2 Then
CompareValues = 1
Else: CompareValues = 2
End If

End Select

End Function


Public Function ListView_GetItemDate(hwnd As Long, lParam As Long) As Date

Dim hIndex As Long
Dim r As Long

objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(hwnd, LVM_FINDITEM, -1, objFind)

objItem.mask = LVIF_TEXT
objItem.iSubItem = m_Col
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)

r = SendMessage(hwnd, LVM_GETITEMTEXT, hIndex, objItem)
If r > 0 Then
ListView_GetItemDate = CDate(Left$(objItem.pszText, r))
End If


End Function


Public Function ListView_GetItemValueStr(hwnd As Long, lParam As Long) As Double

Dim hIndex As Long
Dim r As Long

objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(hwnd, LVM_FINDITEM, -1, objFind)

objItem.mask = LVIF_TEXT
objItem.iSubItem = m_Col
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)

r = SendMessage(hwnd, LVM_GETITEMTEXT, hIndex, objItem)
If r > 0 Then
ListView_GetItemValueStr = CDbl(Left$(objItem.pszText, r))
End If

End Function

Public Function FARPROC(pfn As Long) As Long
FARPROC = pfn

End Function



'//////////////

'//调用

Private Sub lstvAll_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

With lstvAll

m_Col = ColumnHeader.Index - 1



Select Case ColumnHeader.Index - 1
Case Is = 2, 5
'Use sort routine to sort by date
sOrder = Not sOrder
.Sorted = False
SendMessage .hwnd, _
LVM_SORTITEMS, _
.hwnd, _
ByVal FARPROC(AddressOf CompareDates)
Case Is = 3, 6, 7
'Use sort routine to sort by value
sOrder = Not sOrder
.Sorted = False
SendMessage .hwnd, _
LVM_SORTITEMS, _
.hwnd, _
ByVal FARPROC(AddressOf CompareValues)
Case Else
'Use default sorting to sort the items in the list
.SortOrder = 1 - .SortOrder
.Sorted = True
.SortKey = ColumnHeader.Index - 1

End Select
End With

End Sub


回复
lxcc 2002-12-30
如果数据库要排序字段定义是数字就好说!,如果不是,则可能需要数组
回复
ZHAOZHIF 2002-12-30
直接用ORDER BY
回复
lxcc 2002-12-30
好像是不行
可以通过再定义一个数组,把所有的listview的item放到一个二维数组,然后对数组排序,排完后,再把数组重新装入listview
回复
lxcc 2002-12-30
好像是不行
可以通过再定义一个数组,把所有的listview的item放到一个二维数组,然后对数组排序,排完后,再把数组重新装入listview
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告