ORGANIZAR LISTVIEW

 Tópico anterior Próximo tópico Novo tópico

ORGANIZAR LISTVIEW

VB / VBA

 Compartilhe  Compartilhe  Compartilhe
#373112 - 03/05/2011 16:06:46

LEAO10
BRASILIA
Cadast. em:Agosto/2009


Olá galera Vbmaníaca!

Sei que o ListView é possível organizar clicando na descrição de cada item... deve ser fácil, mas fuçando bastante não encontrei como...

Desde já, agradeço

Todos respiramos o mesmo ar, portanto devemos ter todos a mesma chance (Ayrton Senna)

Resposta escolhida #373125 - 03/05/2011 16:54:02

MSMJUDAS
PRESIDENTE PRUDENTE
Cadast. em:Maio/2009


No evento Column_Click do seu ListView coloque isso:

    ListView.SortKey = (ColumnHeader.Index - 1)
    If ListView.SortOrder = lvwAscending Then
        ListView.SortOrder = lvwDescending
    Else
        ListView.SortOrder = lvwAscending
    End If
    ListView.Sorted = True


É uma ordenação bem simples.




#373154 - 04/05/2011 07:05:31



Cadast. em:


Última edição em 04/05/2011 07:11:59 por GOODSPEAKERS

Public Sub SortListView(ListView As ListView, ByVal Index As Integer)

    On Error Resume Next

    If Index = ListView.SortKey + 1 Then
        Dim ascending As Boolean

        If ListView.SortOrder = lvwAscending Then
            ascending = False
        Else
            ascending = True
        End If
    Else
        ascending = True
    End If

    Dim datatype As Byte

    datatype = 0

    Dim LI     As Long

    For LI = 1 To ListView.ListItems.Count

        If Index = 1 Then

            If IsNumeric(ListView.ListItems(LI).Text) Then
                datatype = 1
            ElseIf IsDate(ListView.ListItems(LI).Text) Then
                datatype = 2
            End If

        Else

            If IsNumeric(ListView.ListItems(LI).ListSubItems(Index - 1).Text) Then
                datatype = 1
            ElseIf IsDate(ListView.ListItems(LI).ListSubItems(Index - 1).Text) Then
                datatype = 2
            End If
        End If

    Next LI

    Dim I      As Integer
    Dim l      As Long
    Dim strFormat As String

    ' Display the hourglass cursor whilst sorting

    Dim lngCursor As Long
    lngCursor = ListView.MousePointer
    ListView.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 ListView.hWnd

    Dim blnRestoreFromTag As Boolean

    Select Case datatype
        Case 0

            ' 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

            blnRestoreFromTag = False

        Case 1

            ' Sort Numerically

            strFormat = String$(20, "0") & "." & String$(10, "0")

            ' Loop through the values in this column. Re-format the values so
            ' as they can be sorted alphabetically, having already stored their
            ' text values in the tag, along with the tag's original value

            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 2

            ' Sort by date.

            strFormat = "YYYYMMDDHhNnSs"

            Dim dte As Date

            ' 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 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

    ' Sort the ListView Alphabetically

    ListView.SortOrder = IIf(ascending, lvwAscending, lvwDescending)
    ListView.SortKey = Index - 1
    ListView.Sorted = True

    ' Restore the Text Values if required

    If blnRestoreFromTag Then

        ' 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 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

    ' Unlock the list window so that the OCX can update it

    'LockWindowUpdate 0&

    ' Restore the previous cursor

    ListView.MousePointer = lngCursor




#373155 - 04/05/2011 07:07:06



Cadast. em:


PARA CHAMAR A FUNCAO ACIMA...................

Private Sub lstEstoque_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

    SortListView lstEstoque, ColumnHeader.Index
    lstEstoque.Sorted = False

End Sub




#373162 - 04/05/2011 08:02:55

LEAO10
BRASILIA
Cadast. em:Agosto/2009


Obrigado GOODSPEAKERS...

No momento quero somente uma organização simples...

Valeu MSMJUDAS, é isso mesmo!!!

Todos respiramos o mesmo ar, portanto devemos ter todos a mesma chance (Ayrton Senna)

 Tópico anterior Próximo tópico Novo tópico


Tópico encerrado, respostas não sao permitidas
Encerrado por LEAO10 em 04/05/2011 08:03:21