Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




How To Sort The Item In Listview ?


This code to sort the item in listview only according by Column one.


Code:
ListView1.Sorted = True
How can I let the code work with another column?
Example: it sort according column 4




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Click Item In Listview Then Show That Item In Combo As Index = 0
Hi guys,

I have a listview (lvwUserPunchedOut) that is populated with records in a database (persons first name in one column and last name in the second column), what I am looking to do is if I select a persons name by clicking on it in the listview on frmMain, and then bring up frmSearchCriteria, i would like to show that selected name in the combobox as default (cboEmployee.ListIndex = 0) on frmSearchCriteria (the combobox is sorted = True and is also populated with first name and last name of the person)

How would this be done? Hope I make sense. Thanks in advance.

Sort Vba Listbox After New Item Added
I have a listbox which I want to sort after a new item has beed added. My code is as follows:


Code:
With Name
i = .ListIndex
i = i + 1
.AddItem , i
s = InputBox("Please enter name: ")
If s = "" Then
.RemoveItem i
Else
.List(i, 0) = s

.List(i, 1) = s
End If
End With

Dim vaItems As Variant
Dim p As Long, q As Long
Dim vTemp As Variant

vaItems = Me.Name.List
For p = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For q = p + 1 To UBound(vaItems, 1)
If vaItems(p, 0) > vaItems(q, 0) Then
vTemp = vaItems(p, 0)
vaItems(p, 0) = vaItems(q, 0)
vaItems(q, 0) = vTemp
End If
Next q
Next p

'Clear the listbox
Me.Name.Clear

For p = LBound(vaItems, 1) To UBound(vaItems, 1)
Me.TeamCode.AddItem vaItems(p, 0)
Next p


End Sub

The code works fine until I add the sort code (also found on this forum, I think by Timbo?), it causes a type mismatch error at:
'For p = LBound(vaItems, 1) To UBound(vaItems, 1) - 1'

Any idea what I have done wrong?

Sort Item In A List BOx Of 300 Lines Or More
Hi I need help to sort the following in a List Box :

I am reading four variables, such as Date, Time, Name and Phone-number and saving these in a text file sequentially. I laos append a text 'Date', 'Time' etc repectively on each line for identification. So in one loop I add four text line for these four records.

Then I open the file which has already these type of records in the same order starting with identification of Date, Time etc. I identify these by 'InStr' command and then save these to variables, Date, Time, Name and Number to display in a list box as
'AddItem ListBox1 Name & vbTab & Number & vbTab & Date & vbTab & Time

So this displays the LIst BOx in order that my last read Input is read as last item in List BOx. My quetsion now is first how to display in a revers order. The problem is that when I use

While EOF(1)
Redim Preserve Array(k)
Line Input#1.
.
.

It always read the first line. Is there any way to read the last line first or any other way to display in reverse odrer

My second question is probably tough that how to do sorting now. Is it possible if I can sort the Item w.r.t. Name(alphabetically) or Date(numerically). I tried sorting but It always see the first charactrer in the ItemData and sort it that way. Just to remind that all the four data are added to same Item in List BOx. What sorting mechanism will be suitable for a size of Items of 300 lines or so in List Box.

I have spend more than a week and got help from some Forum posts and but now I think I am lost in woods. Please help me.

Thanks.

Ned

Remove (last) Item From Array And Sort
hey all, me... again :-s

got a quickie here (i think)

ive searched around on the forum and found that if i want to remove
an item from an array and not have a blank space i have to;

find the item (i) to remove
remove the item
make array(i) = array(i + 1)
redim preserve array(nextfreeslot - 2)

but what happens what am i meant to do when ive removed the last
item from the array and the 'nextfreeslot' = 0

i cant redim array() and i cant obviously dim array() again.

so that makes me stuffed.

heeelllppp. lol. thanx!

daniel

How To Sort Combo Box Item With Key Press[Unresolved]...} [Very Urgent]
I have one combo box in main form...i fed combo box with items from access database..all i want is that when i press any key(A to Z), then all the items whose name begins with the key pressed will be listed on combox.
I also want that at first it lists all the items...but when press letter 'S' (suppose), then it will show only items which name begins with letter 'S'.
Please anyone help me out with this
thankx
saracjl

Sabin Kumar Chhetri

Edited by - saracjl on 4/5/2005 4:52:27 AM

Moving A Listview Item And Its Subitems To A New Listview
Hey Guys,

Having a bit of trouble. I am attemping to move a selected item and its subitems from one listview to another. I have no problem getting the contents of the first column, but I can't seem to get the subitems over.

Example: I have this in the first listview:

Code:
|Item |Quanity|Price |PQ |
Bananas 2 $.50 $1.00

Then when I try to move that whole row to the other listview I can only seem to get this:

Code:
|Item |Quanity|Price |PQ |
Bananas

No subitems . Any links or help would be appreciated.

Regards,

Max_Power

ListView Sort
I'm sorting a list view column with a student's grades in it..

so it will show a column with something like the following:

Grades:
100%
85%
92%
66%
72%
98%
100%
43%
0%
54%

When a end user sorts the column by clicking on the header.. it won't show "0 to 100" it will sort by the first number.. so the following would be sorted by:

98%
92%
85%
72%
66%
54%
43%
100%
100%
0%

or

0%
100%
100%
43%
54%
66%
72%
85%
92%
98%


How can I "fix" this to show it in a numerical order? Like this:

0%
43%
54%
66%
72%
85%
92%
98%
100%
100%

or

100%
100%
98%
92%
85%
72%
66%
54%
43%
0%

Listview Re-sort
Hi There... I have a listview that is set to sort on column 1, and when I add rows to the list it sorts ok.

Now when I change a value in column 1 the list dosn't adjust to sort the new value... ie. After I say li.ListSubItems(1).Text = "ZEBRA" is dosn't re-sort the list and put it to the bottom of the list...

I can't find the relevent procedure to get it to re-sort after changing the value... can anyone shed some light on how to do this?

Sort Listview
Quote:





Originally Posted by Lar_19


Agent707: Seems like a lot of extra work when all you need is...
Code:
For i = 0 To lstunsor.ListCount - 1
lstsor.AddItem lstunsor.List(i)
lstsor.ItemData(lstsor.NewIndex) = lstunsor.ItemData(i)
Next i





Ha Lar_19,
Is there a way to sort a single listview?

Listview Sort
Im adding items to the listview that look like this..
Item1 [SomeWord] Some StuffAfter This

Is there a way to sort a listviews items by the letter after the "[" character ( see format above ) during runtime..

Thanks!

Listview Sort
I have a listview that I want to sort automatically by the last column, Can I do this without forcing the user to click the column header?

Listview Sort
When I try to sort a listview box by one of its subitems, a date, it sorts incorrectly, putting days 10 - 30 of the month before days 3-9. The data is retrieved in Short Date format. Does anybody know how to make it sort correctly?

ListView/Sort
Using a lstView control I am attempting to open a non sorted file which includes a listing of cityNames and their salesTotals i.e.......
01 Richmond 40.00
05 Norfolk20.00
01Richmond30.00

I am trying to sort the file prior to processing Section City Totals so that my output looks as such...
01 Richmond 30.00
01 Richmond 40.00
Sec Total 70.00

05 Norfolk 20.00
Sec Total 20.00

Thanks for your help

Listview Sort
Using a Listview with Columns i am able to sort Ascending/Descending. Now my problem is that when i am trying to sort numbers Asc/Desc it sorts it as text, therefore when say sorting 1 to 10 i get the order 1,10,2,3,4, and so on.
My question is how do i sort it as 1,2,3,4, or 10,9,8,7, etc.
I am filling the listview via a database and have tried sorting the database prior to displaying it with not much luck, it obviously can be done as Microsoft do it when sorting file size in Windows Explorer. I am using this code to sort :

Private Sub playerLIST_ColumnClick(ByVal ColumnHeader As ColumnHeader)

playerLIST.SortKey = ColumnHeader.Index - 1
playerLIST.Sorted = True

End Sub

Regards Dazza.

ListView Sort
hi everyone,

i have a listview that im using to hold up to 100 entries, and i did have a bubblesort implemented for the OnColumnClick event to sort integers/decimals both ascending/descending...however, i tested the sort with about 20 entries, and i found that it took way too long!!

ive been told that a QuickSort would be the fastest sort algorithm to implement...but could someone help me out with the code? i have no idea how it would work...thanks!

Is It Possible To Sort A Listview?
I've got a two-column listview that lists teams alphabetically and displays the number of cases associated with them. What if I wanted to sort the teams in descending order, or sort the list by the number of cases? Is this even possible with a listview?

Listview Sort
I have looked throughout the forum but I haven't been able to find the answer. This may be a rather elementary question for most of you, but here is my problem. With the .Add method you have (index, key, text, icon, smallIcon) where "index" is an integer, "key" is supposed to be a unique string (to sort on?), and the text (obvious)......

now this is how my data is turning out ( 4 columns ) only two of which are shown

BILLET

30888 #1
30888 #10
30888 #2

in the add method it works if is pass the key as being:

intSheetNumber & " #" & intBilletNumber ---> this worked

however if I wanted to make the key a composite of the two numbers by padding the billet with zeros it won't work.

strSortKey = intSheetNumber & Format(intBilletNumber, "0000")

and pass strSortKey as the key it won't work?

if anyone can give me some insight it would be greatly appreciated.

ListView And Sort
How can I cause a ListView to sort? How can I send it the instruction to "sort now?"

The Sorted property is set to True, however, when you modify the text of an item, it does not resort. So I need to tell it to resort after the text has been modified.

Any ideas?

How Do I Sort A Listview
I have a form with 3 Option Buttons and a Listview.
My problem is I would like to sort the Listview whenever I click any one of the the Option Buttons. So if I click optBtn2, the Listview would sort by the 2nd column, etc.
How do I do that?

Thanks

Listview Sort
Right, the listview has a sort (sortkey, sortorder and sorted properties). cool, lekker.
but it sorts alphanumerically, and i want numeric sorting (instead of 1, 11, 2 i want 1, 2, 11 etc etc). i can't do this on data retrieval, coz the user should be able to pick a column they wanna sort by

how can this be achieved?

Can Listview Sort
Is listview silly or just me?
I use .SortOrder = lvwAscending, it works fine for string. But it does not sort properly for integer. It sorts like this
11
111
22
222
3
I know it because it treat integer as string, but is some way we can sort integer??? Thanks in advance

Listview Sort
Hi all,
I'm trying to sort a list view control, but I don't want it sorted using string comparison.

I know that a callback function can be used for comparison, but I don't seem to be able to make it work.

Some code would be very helpfull, thanks a lot.

Sort In A Listview
i use this code to make a sort in a listview column, and it works...

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)


If ColumnHeader.Index = 2 Then
ListView1.SortKey = ColumnHeader.Index - 1
End If

End Sub


so . . . i have a question, i have about 10 columns, how can i make a sort inside a sort ?
i mean, if i sort by column 2, and column 2 has the same name a few times, then the next sort will be by column 3.
how can i do that ?

if you have a code example, it will be great.

Sort In LISTVIEW
hi,
  i have project and i use listview in viewing my records i sort the record to ascending and descending but only ascending works and when i click again the descending didn't work and also when i click my refresh button to refresh the listview, only the Record # was shown. the other data was empty. why is that?
  here's my code:

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
Dim colVar As ColumnHeader
    ' If the ListView is already sorted by the clicked column, _
    ' just reverse the order. Otherwise, sort the clicked column ascending.
    If ListView1.Sorted = True And ColumnHeader.SubItemIndex = ListView1.SortKey Then
        If ListView1.SortOrder = ListView1Ascending Then
            ListView1.SortOrder = ListView1Descending
        Else
            ListView1.SortOrder = ListView1Ascending
        End If
    Else
        ListView1.Sorted = True
        ListView1.SortKey = ColumnHeader.SubItemIndex
        ListView1.SortOrder = ListView1Ascending
    End If
    'Now, use the sort information to update the up _
    or down arrows on the columnheader
    For Each colVar In ListView1.ColumnHeaders
        If colVar.SubItemIndex = ListView1.SortKey Then
            If ListView1.SortOrder = ListView1Descending Then
              
            Else
               
            End If
        Else
            
        End If
    Next colVar
End Sub

Private Sub cmdRefresh_Click()
Dim intload As Integer

strsql = "Select *from tblurl"

Set rs = New ADODB.Recordset
    rs.Open strsql, connectstring(), adOpenKeyset, adLockPessimistic, adCmdText
    
ListView1.ListItems.Clear

For i = 1 To ListView1.ListItems.Count
        ListView1.ListItems.Remove 1
Next i

While Not rs.EOF
      intload = intload + 1
      ListView1.ListItems.Add , , rs("Record")
      ListView1.ListItems(intload).ListSubItems.Add , , rs("dtDate")
      ListView1.ListItems(intload).ListSubItems.Add , , rs("Category")
      ListView1.ListItems(intload).ListSubItems.Add , , rs("Type")
      ListView1.ListItems(intload).ListSubItems.Add , , rs("URL")

      rs.MoveNext
      
Wend
End Sub

Listview Disable Sort
I have data stored in such a format:

m1 somthing something else
m2 somthing something else
m3 somthing something else
m4 somthing something else
m5 somthing something else
m6 somthing something else
m7 somthing something else
m8 somthing something else
m9 somthing something else
m10 somthing something else
m11 somthing something else

This information is pulled from an external file, and displayed in a listview, however the listview keeps sorting itself as follows:

m1 somthing something else
m10 somthing something else
m11 somthing something else
m2 somthing something else
m3 somthing something else
m4 somthing something else
m5 somthing something else
m6 somthing something else
m7 somthing something else
m8 somthing something else
m9 somthing something else

Notice that M10 & M11 are not in the correct position!

I have turned the sorted to FALSE, but its still does not display the items correctly in the listview. I want the lsitview to display the value exactly the same as as it appears in external document.

Any ideas? Many thanks for your assistance.

Rgds,
Daniel.

Listview Sort Items
Hi,

how can I move Items in a ListView with an up and down button?

Sort More Than One Column In Listview
I am using the follwoing code to sort a list view column

ListView1.SortOrder = lvwDescending
ListView1.SortKey = 9
ListView1.Sorted = True


I am trying to sort a league table so some time the points will be the same so I then need them to be sorted by the goal differance column.

ListView Sort Indicators
Hello!

I was wondering if it is possible to get the default sort indicators to appear on the column headers. I have an example for doing that, but it's for C: http://www.codeguru.com/listview/ind...rt_order.shtml

Regards,
Sebastian Mares

PS: I already have the sorting code - I only need to make the indicators appear.

Listview Sort Problem
helloi
i have a listview like that

ali 120
veli 200
deli 140
musta 170


when i use the listview sort ,the strings (ali,veli..) sorted truly.
But when i try to sort numbers, it didinot work truly.
What should i do for that?

Virtua Listview Sort
does anyone know if its possible to sort a virtual listview when you click a column? I've been fooling around with it but haven't had any luck, so i'm not even sure it would be possible since technically there are no items in the listview.

Sort Listview By Date
ok i searched this and found no explination for my question,

within access i have a query and this query contains date data. the field format is set to Date/Time and the query is set to sort the dates in an ascending order, and does this well. however when i load this query into my listview, hoping it would load in the same order as the access query, the listview displays the dates out of order. why? and how can i make the dates be displayed in order in my listview?

i thought that when opening a table or query from VB6, the data was read in the same order i see it on screen. is this incorrect? if i view a query and it is sorted correctly, then why is it load into a listview incorrectly?

Sort The Record In The Listview
I have the record in the listview. The problem is the record in the listview is not sort . I would like to sort by ascending the start and end date...



Code:
On Error Resume Next
ListView1.ListItems.Clear

Dim newDate As Date
Dim newDate1 As Date

Dim sql As String



Call OpenConnection
rec.Close


sql = "SELECT * FROM kerja WHERE start_date >= #" & Format(DTPicker3.Value, "yyyy-MM-dd") & "# and end_date >= #" & Format(DTPicker3.Value, "yyyy-MM-dd") & "# or start_date <= #" & Format(DTPicker3.Value, "yyyy-MM-dd") & "# and end_date >= #" & Format(DTPicker3.Value) & "# "

rec.Open sql, con, adOpenDynamic, adLockOptimistic
rec.MoveFirst

Do Until rec.EOF

newDate = DateAdd("d", rec.Fields("hari").Value, DTPicker1.Value)

rec.Fields("start_date").Value = newDate


newDate1 = DateAdd("d", rec("peruntukan") - 1, rec("start_date"))
rec.Fields("end_date").Value = newDate1





Set lvItem = ListView1.ListItems.Add(, , "" & Format(newDate, "dd/MM/yyyy"))
lvItem.ListSubItems.Add , , "" & (Format(newDate1, "dd/MM/yyyy"))




'Set lvItem = lvJadual1.ListItems.Add(, , "" & rec("start_date"))

'lvItem.ListSubItems.Add , , "" & rec("end_date")
lvItem.ListSubItems.Add , , "" & rec.Fields("peruntukan").Value
lvItem.ListSubItems.Add , , "" & rec.Fields("aktiviti").Value
lvItem.ListSubItems.Add , , "" & rec.Fields("sasaran").Value
lvItem.ListSubItems.Add , , "" & rec.Fields("jenisinput").Value
lvItem.ListSubItems.Add , , "" & rec.Fields("tindakan").Value


rec.Update

rec.MoveNext
Loop

Listview Multi Col Sort ?
I have a list view.. has columns.. year, manufacturer,model,cab version etc..

right now I have the sort as col 0.. so by year.. but its all over the place,
I want it to sort year.. then kinda subsort on manufacturer, then model etc

there a way to do this ? I didnt see any posts.. looks like sort is on
one column only ..

ListView Sort Order
Hi,

In my application i am sorting ListView1 by ID but it is sorting in the way windows does (See Image Below)



Any ideas of how i can stop this happening?

Ross

Sort Listview Query
I found the code below in the forum and i copy and pasted it in my app.
I want to sort the listview column where the user has clicked. If I have 7 columns, and the user clicked on the 2nd column, the listview will sort Ascending in the 2nd column and the rest of the the column will follow.

Example column:
12 de
32 ed
23 ab
41 ba

if user will click column 1 the result will be:
12 de
23 ab
32 ed
41 ba

if column is clicked, the result will be:
23 ab
41 ba
12 de
32 ed

My code before i pasted the code (which sorts the column but not the way I want it to be)

VB Code:
Private Sub lstRecords_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)  If lstRecords.SortOrder = lvwAscending Then    lstRecords.SortOrder = lvwDescending  Else    lstRecords.SortOrder = lvwAscending  End IfEnd Sub


Code from forum. When I run this, I get the error I attached.
Also I am wondering when I double click my listview I dont have Index As Integer. What kind of control is used here?

VB Code:
Private Sub lstRecords_ColumnClick(Index As Integer, ByVal ColumnHeader As MSComctlLib.ColumnHeader)  Static SortAsc As Boolean   If blnFirstTime Then    SortAsc = False    blnFirstTime = False  Else    SortAsc = Not SortAsc  End If      With lstRecords(Index)    .SortKey = ColumnHeader.Index - 1    If SortAsc Then      .SortOrder = lvwAscending    Else      .SortOrder = lvwDescending    End If      .Sorted = True    End WithEnd sub

Sort Dates In Listview
Hi to all:

I have this code:


VB Code:
Private Sub ListView1_ColumnClick(Index As Integer, ByVal ColumnHeader As ComctlLib.ColumnHeader)ListView1(Index).Sorted = TrueListView1(Index).SortKey = ColumnHeader.Index - 1End Sub


One of the column have dates,and this code don't align the dates in listview...

The question is:
The Listview don't align dates?Or somthing else wrong in the code?

Thanks

Listview Sort Question:
how can I sort a column in a listview if its numbers? it does it the "text" way...

??

Listview Sort By Clicking..
Sometimes, ive had it where you click the top of a column in a listview, and it has an arrow pointing up and down. I was wondering if anyone could tell me how to use this thing?

Thanks

How Can I Sort A Listview Control?
hello everyone

the listview control in visual basic can sort on any column simply. my problem is that it behaves like it is sorting strings. i cannot get it to correctly sort on numbers or dates.

eg. it sorts numbers 1 to 23 like this....

1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
3
4
5
6
7
8
9

same problem with dates it thinks they are strings. i have investigated the following:
(*) the columnheader objet does not have a property where i can define the data type of the column values that it will hold
(*) you cannot create a hidden column to sort on. ie. somehow fake the sort

the windows explorer uses a similar or probably the same control. click on the size and see the column values are not sorted as if they were strings, they are actually sorted in correct order of file size.

ANY IDEAS/SOLUTION/ALTERNATIVE.


also see the control (listview) used by winzip. do you think these are the same as what we the vb-developers have been provided with.


[This message has been edited by badal (edited 01-28-2000).]

Possible To Sort A Listview By More Than One Column?
Does anyone know if its possible to do a multi-column sort on a listview? ie....Sort column 1 as primary sort column then column 2 as secondary sort colmn.....hmmmm?

Sort Column On A Listview
It is possible to sort a column on a listview just doing click on the column?

Date Sort In ListView
I have a listview in Report mode, the 1st column contains date values, but they are sorted as a string

how can i get around this?

NB: without formatting to yyyymmdd, coz it looks crap like that

Listview Sort Problems
I have a listview which i populate from a 3D Array. Its fine unless i try to sort it using the .Sorted = True, but when i do my data is mucked up, i only get the first coloum, and the last record only has all its data in, ive tried before, after populating the data and in design time changing it to sorted but it still does the same :/



ideas?

Sort A Listview But Not Alphabetically
Hello all,
I'm working with a listview. I need to sort the listview in this order. (These are names of data types and they are stored in one of the listview's columns)

BLKD
DBLE
BLKR
REAL
BLI4
INT4
BLI2
INT2
BLI1
INT1
BLL1
LOG1

As you can see, they are not alphabetical. There are approx. 20,000 to 30,000 items in the listview, so you can understand my need for an efficient algorithm.

I've considered using an enumeration, but apparently REAL is a keyword in VB.

Help greatly appreciated.

Cedric

ListView Sort Option (A-Z.. Or Z-A)
The option Listview.Sorted = true
sorts the listview content, but only a-z, or 0-9

in my case, i need the listview to be sorted from 9-0, the opposite, as i'm sorting dates..

any idea's?

cheers

Random Sort A Listview?
Does anyone know how to use rnd to sort the items in a listview?

Thanks, Bennie

ListView Special Sort
I have a listview I want to sort, the problem is that I need to sort the list in a certain way i.e. SD,D,S.

Any ideas??

Sort ListView Checkboxes
How do you sort a listView by the checkBox?
I just figured how how to sort my listView

I have three fields.
Checkbox, Organization, and Bank (the bank is hidden so user only sees the CheckBox Field and Organization)

-This sorts the organization
Private Sub lvwOrganization_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
        lvwOrganization.SortOrder = (lvwOrganization.SortOrder + 1) Mod 2
        lvwOrganization.SortKey = ColumnHeader.Index - 1
        lvwOrganization.Sorted = True
End Sub

How do you sort it by the check boxes?
For example say there are 6 records 3 are checked and 3 are not and once the user clicks the header for the
check boxes all the items that are checked sort together and all the items that are not checked sort together.

Sort Problem In Listview
Hi,

My purpose is simple. when the first time the data is displayed, lines are while and then gray and then white ...

But every time when the listview displays data, sometimes it is right (one while one gray), sometimes it is wrong - several whites or grays are grouped. I do not know why.

Any ideas? Thanks!


Gavin

Copyright © 2005-08 www.BigResource.com, All rights reserved