每列可以排序,例如显示文字的列,显示金额的列(如何正确进行索引?)
解决方案 »
- 添加记录的问题。初学者,急!!!
- 周未有人在吗?请问一下。浏览器 myie2 或 腾讯浏览器 在同一窗口中打开多个网页是怎么实现的!
- 为什么我的 collection.add执行多次后collection.count还是等于1~!!!
- 帮助文件如何调用?
- 怎样读取注册表中指定项目的键值?(在线等待)
- 一个简单的datagrid问题,请各位帮手解决下。
- 关于可插入对象excel图表的 问题
- 计算机与单片机串口232通迅奇怪问题,求高手帮忙
- 怎么用run的命令进行调用 .exe软件
- 用过或者知道RegisterServiceProcess的进来,我有困难!
- datagrid表格问题?
- 如何获取IE下载对话框(另存为)中的默认文件名?
lvwVar.ListItems.Add , , Format(tail, "yyyy-mm-dd hh:mm:ss")
Else
lvwVar.ListItems.Add , , tail
End If
不然就是如下情况
1
11
12
2
10日期的那个format成为 yyyymmdd就好了
' Win32 API
'----------------------------------------------------------------Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As LongPrivate Declare Function GetTickCount Lib "kernel32" () As Long'****************************************************************
' Form_Load
' Initialises the list control, and populates it with random data
'----------------------------------------------------------------Private Sub Form_Load()
With ListView1
' Add three columns to the list - one for each data type.
' Note that the data type is set in the column header's
' tag in each case
.ColumnHeaders.Add(, , "String").Tag = "STRING"
.ColumnHeaders.Add(, , "Number").Tag = "NUMBER"
.ColumnHeaders.Add(, , "Date").Tag = "DATE"
' Set the column alignment - has no bearing on the sorts.
.ColumnHeaders(1).Alignment = lvwColumnLeft
.ColumnHeaders(2).Alignment = lvwColumnRight
.ColumnHeaders(3).Alignment = lvwColumnCenter
' Populate the list with data
Dim l As Long
Dim dblRnd As Double
Dim dteRnd As Date
With .ListItems
For l = 1 To 1000
With .Add(, , "ListItem " & Format(l, "0000"))
dblRnd = (Rnd() * 10000) - 5000
dteRnd = (Rnd() * 1000) + Date
.ListSubItems.Add , , Format(dblRnd, "0.00")
.ListSubItems.Add , , Format(dteRnd, _
"dd/mm/yyyy")
End With
Next l
End With
End With
End Sub
MSComctlLib.ColumnHeader) On Error Resume Next
' Record the starting CPU time (milliseconds since boot-up)
Dim lngStart As Long
lngStart = GetTickCount
' Commence sorting
With ListView1
' Display the hourglass cursor whilst sorting
Dim lngCursor As Long
lngCursor = .MousePointer
.MousePointer = vbHourglass
' Prevent the ListView control from updating on screen -
' this is to hide the changes being made to the listitems
' and also to speed up the sort
LockWindowUpdate .hWnd
' Check the data type of the column being sorted,
' and act accordingly
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"
' Sort by date.
strFormat = "YYYYMMDDHhNnSs"
' Loop through the values in this column. Re-format
' the dates so as they can be sorted alphabetically,
' having already stored their visible values in the
' tag, along with the tag's original value
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
' Sort the list alphabetically by this column
.SortOrder = (.SortOrder + 1) Mod 2
.SortKey = ColumnHeader.Index - 1
.Sorted = True
' Restore the previous values to the 'cells' in this
' column of the list from the tags, and also restore
' the tags to their original values
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
Case "NUMBER"
' Sort Numerically
strFormat = String(30, "0") & "." & String(30, "0")
' Loop through the values in this column. Re-format the values so as they
' can be sorted alphabetically, having already stored their visible
' values in the tag, along with the tag's original value
With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
.Tag = .Text & Chr$(0) & .Tag
If IsNumeric(.Text) Then
If CDbl(.Text) >= 0 Then
.Text = Format(CDbl(.Text), _
strFormat)
Else
.Text = "&" & InvNumber( _
Format(0 - CDbl(.Text), _
strFormat))
End If
Else
.Text = ""
End If
End With
Next l
Else
For l = 1 To .Count
With .Item(l)
.Tag = .Text & Chr$(0) & .Tag
If IsNumeric(.Text) Then
If CDbl(.Text) >= 0 Then
.Text = Format(CDbl(.Text), _
strFormat)
Else
.Text = "&" & InvNumber( _
Format(0 - CDbl(.Text), _
strFormat))
End If
Else
.Text = ""
End If
End With
Next l
End If
End With
' Sort the list alphabetically by this column
.SortOrder = (.SortOrder + 1) Mod 2
.SortKey = ColumnHeader.Index - 1
.Sorted = True
' Restore the previous values to the 'cells' in this
' column of the list from the tags, and also restore
' the tags to their original values
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
' Sort alphabetically. This is the only sort provided
' by the MS ListView control (at this time), and as
' such we don't really need to do much here
.SortOrder = (.SortOrder + 1) Mod 2
.SortKey = ColumnHeader.Index - 1
.Sorted = True
End Select
' Unlock the list window so that the OCX can update it
LockWindowUpdate 0&
' Restore the previous cursor
.MousePointer = lngCursor
End With
' Report time elapsed, in milliseconds
MsgBox "Time Elapsed = " & GetTickCount - lngStart & "ms"
End Sub'****************************************************************
' InvNumber
' Function used to enable negative numbers to be sorted
' alphabetically by switching the characters
'----------------------------------------------------------------Private Function InvNumber(ByVal Number As String) As String
Static i As Integer
For i = 1 To Len(Number)
Select Case Mid$(Number, i, 1)
Case "-": Mid$(Number, i, 1) = " "
Case "0": Mid$(Number, i, 1) = "9"
Case "1": Mid$(Number, i, 1) = "8"
Case "2": Mid$(Number, i, 1) = "7"
Case "3": Mid$(Number, i, 1) = "6"
Case "4": Mid$(Number, i, 1) = "5"
Case "5": Mid$(Number, i, 1) = "4"
Case "6": Mid$(Number, i, 1) = "3"
Case "7": Mid$(Number, i, 1) = "2"
Case "8": Mid$(Number, i, 1) = "1"
Case "9": Mid$(Number, i, 1) = "0"
End Select
Next
InvNumber = Number
End Function'****************************************************************
'
'----------------------------------------------------------------
http://www.codeguru.com/vb/controls/vb_listview/article.php/c1669/
对数字/字符/日期排序。