USUÁRIO:      SENHA:        SALVAR LOGIN ?    Adicione o VBWEB na sua lista de favoritos   Fale conosco 

 

  Dicas

  Visual Basic    (Grid/FlexGrid)

Título da Dica:  Ordenar Listview de forma inteligente (data, número e/ou texto)
Postada em 9/9/2004 por PC²            
'coloque esta declaração como global
Dim OrdAsc as boolean 'serve para poder classificar como ascendente/descendente

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
'Usaremos o "Tag" de cada ColumHeader para armazenar
'se a coluna recebe valores:
'       = N para Números
'       = D para Datas
'       = T para Textos

If ColumnHeader.Tag = "N" Then '
    SortListView ListView1, ColumnHeader.Index, ldtNumber, OrdAsc
ElseIf ColumnHeader.Tag = "T" Then
    SortListView ListView1, ColumnHeader.Index, ldtString, OrdAsc
Else
    SortListView ListView1, ColumnHeader.Index, ldtDateTime, True
End If
OrdAsc = Not OrdAsc
End Sub

'Cole esta função dentro de um MÓDULO:
'Há algumas API's
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Public Sub SortListView(ListView As ListView, ByVal Index As Integer, _
                ByVal DataType As ListDataType, ByVal Ascending As Boolean)

    On Error Resume Next
    Dim i As Integer
    Dim l As Long
    Dim strFormat As String
    Dim lngCursor As Long
    lngCursor = ListView.MousePointer
    ListView.MousePointer = vbHourglass
    LockWindowUpdate ListView.hwnd
    
    Dim blnRestoreFromTag As Boolean
    
    Select Case DataType
    Case ldtString
        blnRestoreFromTag = False
        
    Case ldtNumber
        strFormat = String$(20, "0") & "." & String$(10, "0")
        With ListView.ListItems
            If (Index = 1) Then
                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
            Else
                For l = 1 To .Count
                    With .Item(l).ListSubItems(Index - 1)
                        .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
        
        blnRestoreFromTag = True
    
    Case ldtDateTime
        strFormat = "YYYYMMDDHhNnSs"
        
        Dim dte As Date
      
        With ListView.ListItems
            If (Index = 1) Then
                For l = 1 To .Count
                    With .Item(l)
                        .Tag = .Text & Chr$(0) & .Tag
                        dte = CDate(.Text)
                        .Text = Format$(dte, strFormat)
                    End With
                Next l
            Else
                For l = 1 To .Count
                    With .Item(l).ListSubItems(Index - 1)
                        .Tag = .Text & Chr$(0) & .Tag
                        dte = CDate(.Text)
                        .Text = Format$(dte, strFormat)
                    End With
                Next l
            End If
        End With
        
        blnRestoreFromTag = True
        
    End Select
      
    ListView.SortOrder = IIf(Ascending, lvwAscending, lvwDescending)
    ListView.SortKey = Index - 1
    ListView.Sorted = True
        
    If blnRestoreFromTag Then
                
        With ListView.ListItems
            If (Index = 1) Then
                For l = 1 To .Count
                    With .Item(l)
                        i = InStr(.Tag, Chr$(0))
                        .Text = Left$(.Tag, i - 1)
                        .Tag = Mid$(.Tag, i + 1)
                    End With
                Next l
            Else
                For l = 1 To .Count
                    With .Item(l).ListSubItems(Index - 1)
                        i = InStr(.Tag, Chr$(0))
                        .Text = Left$(.Tag, i - 1)
                        .Tag = Mid$(.Tag, i + 1)
                    End With
                Next l
            End If
        End With
    End If
        
    LockWindowUpdate 0&
        
    ListView.MousePointer = lngCursor

End Sub
 


CyberWEB Network Ltda.    © Copyright 2000-2024   -   Todos os direitos reservados.
Powered by HostingZone - A melhor hospedagem para seu site
Topo da página