List View click on header to sort date column 
Author Message
 List View click on header to sort date column
Hi

l have found a sample code in MSDN to for sorting a list view header date
column,

however it populates the listview differently to mine.

Can some expert use mine list view population and MSDN list view sort  code

Thanks inadvance

Clive

Mine listview population
=================

    'Open the recordset.
    rsADOOrderSummary.Open "tblTemp", cnn, adOpenKeyset, adLockOptimistic,
adCmdTable

    'Displaying current Orders
    If rsADOOrderSummary.RecordCount <> 0 Then
        With rsADOOrderSummary
            Do While Not .EOF
                Set itmx = lvwOrder.ListItems.Add()

                itmx.Text = !OrderNo & ""
                itmx.SubItems(1) = !CustomerName & ""
                itmx.SubItems(2) = !Jobnumber & ""
                itmx.SubItems(3) = !Value & ""
                itmx.SubItems(4) = Format(!OrderDate, "dd/mm/yy")
                itmx.SubItems(5) = Format(!DeliveryDate, "dd/mm/yy")
                rsADOOrderSummary.MoveNext
            Loop
        End With
    Else
        MsgBox "No Orders in database", vbOKOnly + vbExclamation 'Msg_Title
        Exit Sub
    End If

MSDN Example to sort a listview with date column
===================================
NOTE: This project fails in Visual Basic 6.0 if you use the Microsoft
Windows Common Controls 6.0 instead of Microsoft Windows Common Controls
5.0.

      Option Explicit

      Private Sub Form_Load()

        Dim clmAdd As ColumnHeader
        Dim itmAdd As ListItem

        'Add two Column Headers to the ListView control
        Set clmAdd = ListView1.ColumnHeaders.Add(Text:="Name")
        Set clmAdd = ListView1.ColumnHeaders.Add(Text:="Date")

        'Set the view property of the Listview control to Report view
        ListView1.View = lvwReport

        'Add data to the ListView control
        Set itmAdd = ListView1.ListItems.Add(Text:="Joe")
        itmAdd.SubItems(1) = "05/07/97"

        Set itmAdd = ListView1.ListItems.Add(Text:="Sally")
        itmAdd.SubItems(1) = "04/08/97"

        Set itmAdd = ListView1.ListItems.Add(Text:="Bill")
        itmAdd.SubItems(1) = "05/29/97"

        Set itmAdd = ListView1.ListItems.Add(Text:="Fred")
        itmAdd.SubItems(1) = "05/17/97"

        Set itmAdd = ListView1.ListItems.Add(Text:="Anne")
        itmAdd.SubItems(1) = "04/01/97"

      End Sub

      Private Sub ListView1_ColumnClick(ByVal ColumnHeader As _
              ComctlLib.ColumnHeader)
        Dim strName As String
        Dim dDate As Date
        Dim lngItem As Long

        'Handle User click on column header
        If ColumnHeader.Text = "Name" Then  'User clicked on Name header
          ListView1.Sorted = True        'Use default sorting to sort the
          ListView1.SortKey = 0          'items in the list
        Else
          ListView1.Sorted = False       'User clicked on the Date header
                                         'Use our sort routine to sort
                                         'by date
          SendMessage ListView1.hWnd, _
                      LVM_SORTITEMS, _
                      ListView1.hWnd, _
                      AddressOf CompareDates
        End If

        'Refresh the ListView before writing the data
        ListView1.Refresh

        'Loop through the items in the List to print them out in
        'sorted order.
        'NOTE: You are looping through the ListView control because when _
        'sorting by date the ListItems collection won't be sorted.

        For lngItem = 0 To ListView1.ListItems.Count - 1
          ListView_GetListItem lngItem, ListView1.hWnd, strName, dDate
        Next

      End Sub

Module Code

      Option Explicit

      'Structures
      Public Type POINT
        x As Long
        y As Long
      End Type

      Public Type LV_FINDINFO
        flags As Long
        psz As String
        lParam As Long
        pt As POINT
        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 Long
        cchTextMax As Long
        iImage As Long
        lParam As Long
        iIndent As Long
      End Type

      'Constants
      Private Const LVFI_PARAM = 1
      Private Const LVIF_TEXT = &H1

      Private Const LVM_FIRST = &H1000
      Private Const LVM_FINDITEM = LVM_FIRST + 13
      Private Const LVM_GETITEMTEXT = LVM_FIRST + 45
      Public Const LVM_SORTITEMS = LVM_FIRST + 48

      'API declarations
      Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long

      'Module Functions and Procedures
     ' NOTE: This project fails in Visual Basic 6.0 if you use the Microsoft
Windows Common Controls 6.0 instead of Microsoft Windows Common Controls
5.0.

      'CompareDates: This is the sorting routine that gets passed to the
      'ListView control to provide the comparison test for date values.

      Public Function CompareDates(ByVal lngParam1 As Long, _
                                   ByVal lngParam2 As Long, _
                                   ByVal hWnd As Long) As Long

       Dim strName1 As String
       Dim strName2 As String
       Dim dDate1 As Date
       Dim dDate2 As Date

        'Obtain the item names and dates corresponding to the
        'input parameters

        ListView_GetItemData lngParam1, hWnd, strName1, dDate1
        ListView_GetItemData lngParam2, hWnd, strName2, dDate2

        'Compare the dates
        'Return 0 ==> Less Than
        '       1 ==> Equal
        '       2 ==> Greater Than

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

      End Function

      'GetItemData - Given Retrieves
      Public Sub ListView_GetItemData(lngParam As Long, _
                                      hWnd As Long, _
                                      strName As String, _
                                      dDate As Date)
        Dim objFind As LV_FINDINFO
        Dim lngIndex As Long
        Dim objItem As LV_ITEM
        Dim baBuffer(32) As Byte
        Dim lngLength As Long

        ' Convert the input parameter to an index in the list view
        objFind.flags = LVFI_PARAM
        objFind.lParam = lngParam
        lngIndex = SendMessage(hWnd, LVM_FINDITEM, -1, VarPtr(objFind))

        '
        ' Obtain the name of the specified list view item
        '
        objItem.mask = LVIF_TEXT
        objItem.iSubItem = 0
        objItem.pszText = VarPtr(baBuffer(0))
        objItem.cchTextMax = UBound(baBuffer)
        lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
                                VarPtr(objItem))
        strName = Left$(StrConv(baBuffer, vbUnicode), lngLength)

        ' Obtain the modification date of the specified list view item

        objItem.mask = LVIF_TEXT
        objItem.iSubItem = 0
        objItem.pszText = VarPtr(baBuffer(0))
        objItem.cchTextMax = UBound(baBuffer)
        lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
                                VarPtr(objItem))
        If lngLength > 0 Then
          dDate = CDate(Left$(StrConv(baBuffer, vbUnicode), lngLength))
        End If

      End Sub

      'GetListItem - This is a modified version of ListView_GetItemData
      ' It takes an index into the list as a parameter and returns
      ' the appropriate values in the strName and dDate parameters.

      Public Sub ListView_GetListItem(lngIndex As Long, _
                                      hWnd As Long, _
                                      strName As String, _
                                      dDate As Date)
        Dim objItem As LV_ITEM
        Dim baBuffer(32) As Byte
        Dim lngLength As Long

        ' Obtain the name of the specified list view item
        objItem.mask = LVIF_TEXT
        objItem.iSubItem = 0
        objItem.pszText = VarPtr(baBuffer(0))
        objItem.cchTextMax = UBound(baBuffer)
        lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
                                VarPtr(objItem))
        strName = Left$(StrConv(baBuffer, vbUnicode), lngLength)

        ' Obtain the modification date of the specified list view item
        objItem.mask = LVIF_TEXT
        objItem.iSubItem = 0
        objItem.pszText = VarPtr(baBuffer(0))
        objItem.cchTextMax = UBound(baBuffer)
        lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
                                VarPtr(objItem))
        If lngLength > 0 Then
          dDate = CDate(Left$(StrConv(baBuffer, vbUnicode), lngLength))
        End If

      End Sub



Wed, 05 Feb 2003 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Sorting the content of list view on clicking column headers

2. List View - Sort by Clicking Header

3. Display Bitmaps in Sub Items and column Headers of List View During Report View

4. sort a listview by column header click

5. ? 3014 Can't open any more tables - Click header to sort by column fails

6. sort ListView by date and time by pressing the column header

7. List View Column Headers

8. Placing an image in a list view's column header

9. Placing an image in a list view's column header

10. List View Column Sorting

11. Logical sorting in List View (date and/or time)

12. Sorting List View Control on date field??

 

 
Powered by phpBB® Forum Software