• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 748
  • Last Modified:

ListView Sort problem

Hi Experts
I using ListView for listing my data. There is some alphabetic & numeric field.
To sort numeric field I used the following code.Yes it sort accordingly.
BUT THE PROBLEM IS AFTER SORT THIS NUMERIC FIELD I USED FOR LOOP TO GET THE DATA FROM LISTVIEW BUT IT NOT GETTING AS SHOWN IN LISTVIEW.
how can I get that same from  listview (there is no problem for alphabetic feild)

'for getting data from listview, it is not same as shown 'in listview after we click numeric field header
For I = 1 To .ListItems.Count
   strAPPNO = .ListItems(I).ListSubItems(2).Text
   strAPPNO = strAPPNO & Space(10 - Len(strAPPNO))
   Msgbox strAPPNO
Next

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
sOrder = Not sOrder
ListView1.SortKey = ColumnHeader.Index - 1
SortItemIdx = ColumnHeader.Index - 1

    Select Case ColumnHeader.Index - 1
       Case 1, 2, 3:
             ListView1.SortKey = SortItemIdx
             ListView1.SortOrder = Abs(Not ListView1.SortOrder = 1)
             ListView1.Sorted = True
       Case 4:
             ListView1.Sorted = False
             SendMessage ListView1.hWnd, LVM_SORTITEMS, ListView1.hWnd, ByVal PROC(AddressOf CompareValues)
    End Select
End Sub

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

   Dim val1 As Long
   Dim val2 As Long
     
   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_GetItemValueStr(hWnd As Long, lParam As Long) As Long

   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 = SortItemIdx
   objItem.pszText = Space$(32)
   objItem.cchTextMax = Len(objItem.pszText)
   r = SendMessage(hWnd, LVM_GETITEMTEXT, hIndex, objItem)
   If r > 0 Then
      ListView_GetItemValueStr = CLng(Left$(objItem.pszText, r))
   End If

End Function

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

0
anij
Asked:
anij
  • 4
  • 2
1 Solution
 
ArkCommented:
Hi
 VB listView control sort items in alphabetical order only, so numeric values sorting like this:
1
11
2 etc
Once you're using API to sort values, you have to continue ALL processe with API, because API sort items in listview, but not in VB ListItems collection.
Instead of using API, you can make numeric values "right justified" before sorting:

For I = 1 To .ListItems.Count
  strAPPNO = .ListItems(I).ListSubItems(2).Text
  .ListItems(I).ListSubItems(2).Text = Space(10 - Len(strAPPNO)) & strAPPNO
Next

In this case all numeric items will sort correctly and you'll have access to ListItems collection.

Cheers
0
 
anijAuthor Commented:
Ark
Tell me How to change VB ListItem or How can get data from
ListView (API)
I think
plz, Help me ASAP. I am in very urgent
0
 
anijAuthor Commented:
Hi Ark,
How to get the data from listview (Not VB ListItem, OK) after API sort.
plz, Help Me. Very urgent

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ArkCommented:
Hi
You can use LVM_GETITEM = (LVM_FIRST + 5) message:

Public Function ListView_GetItem(lvhWnd As Long, pitem As LVITEM) As Boolean
  ListView_GetItem = SendMessage(lvhWnd, LVM_GETITEM, 0, pitem)
End Function
where pitem structure defined as follow:

' LVM_GET/SETITEM lParam
Public Type LVITEM   ' was LV_ITEM
  mask As Long
  iItem As Long
  iSubItem As Long
  state As Long
  stateMask As Long
  pszText As Long  ' if String, must be pre-allocated  before filled
  cchTextMax As Long
  iImage As Long
  lParam As Long
#If (WIN32_IE >= &H300) Then
  iIndent As Long
#End If
End Type

But more easy is to fill ALL items and subitems with strings without using API, adding spaces before numerig items:
was:
1
2
3
11
22
111
222
etc.
In this case vb listview sort items as following:
1,11,111,2,22,222,3.
But if you add spaces (note that space have ASCII = 32< any numeric value):
  1
  2
  3
 11
 22
111
222
vb ListView sort it like 1,2,3,11,22,111,222

For example, if your numeric values don't exeed 10 digits
Instead of ListView1.ListItems.Add myValue
use
ListView1.ListItems.Add Space(10 - Len(cStr(myVal))) & cStr(myVal)

Cheers


0
 
anijAuthor Commented:
Ark Thanx
I will check it.
You not checked this link.
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20096375
There I asked whether we can create our process as child process of another process ?
Is possible ? I will ask a valuable question.
plz, respond
0
 
anijAuthor Commented:
This is another link for the same problem.
It is looks like ur answer.
I will give u point after check it
OK
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10170794
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now