Hi group, I have a populated listbox, and I should sort the data by clicking on the “Lordo F.” column, but it sorts the data partially.
How do I solve it?
By reading the documentation and implementing your own sort order …
BTW: you do not tell us from where the data comes: text file or db query ?
It is possible to make a next column and you sort this
The next column if formated whit format ( value , “0000” )
Yes, it is also possible to ask someone to do the job for you… some will do (for $, of course).
Back to basics.
The documentation DesktopListBox have a part on that subject. Search for:
Sorting rows with your own algorithm
there is even code to achieve that.
And if your data comes from a db query, look at the Database documentation to learn how to do that directly.
You can even ignore the Sort ask. (why sorting paiements ?)
PS: do you know that you can align the column contents (Netto F.
and Lordo F.
).
You can also replace true
with some better UI. Someone with a GOOD idea ?
PPS: to Sort your dates, place ISO dates in ColumnTag(0) and sort based on these values to avoid month mix-up.
Check out comparerows event. Discussion here:
The explanation is simple: applying string sorting to numbers gives these results.
So, the subject name of the forum entry above is perfect.
Your Number Column will by standard be sorted in alphabetical order. You need to “override” this order by using the RowComparison Event.
the cells in the listbox are strings, you can use the cell tag for real value and use it in the RowComparison method or convert string back to number for the cell so you can use < > =
Another different approach:
The numbers are already not aligned.
I suggest that you set the font of the listbox to be a fixed width font, such as Courier.
Then, when you fill the listbox, pad the numbers using spaces.
Instead of
Celltextat(x,y) = format(v,"0.00")
try
var s as string
s = " " + format(v,"0.00")
Celltextat(x,y) = s.right(10)
That way your numbers will align as they should, and they will also sort as they should, like this:
. 123,45
. 9324,31
. 53,10
Can’t this be better done by setting ColumnAlignmentAt for the column in question?
Seems to me the OP would do better to read the documentation and try things for himself.
To keep it simple, I would rather vote for a decimal alignment (+ offset) and RowComparison sorting.
Very good point. I had forgotten about that for the visuals.
Thanks to all, I didn’t know you could say how to sort a listbox, I thought it had to be done either by code with the query or it was the sorting given by the listbox itself. I discovered RowComparision, I read a little and I wrote this code, which sorts my table when I click on the columns that interest me, sorting the results correctly. What do you think? Any suggestions?
In my ListBox → Rowcomparision →
Select Case column
Case 4
'messagebox "Sto ordinando per TOTALE..." + me.CellTextAt(row1, column).Val.ToString
If Me.CellTextAt(row1, column ).Val < Me.CellTextAt(row2, column).Val Then
result = -1
ElseIf Me.CellTextAt(row1, column).Val > Me.CellTextAt(row2, column).Val Then
result = 1
Else
result = 0
End If
Return True
Case 6
'messagebox "Sto ordinando per TOTALE..." + me.CellTextAt(row1, column).Val.ToString
If Me.CellTextAt(row1, column ).Val < Me.CellTextAt(row2, column).Val Then
result = -1
ElseIf Me.CellTextAt(row1, column).Val > Me.CellTextAt(row2, column).Val Then
result = 1
Else
result = 0
End If
Return True
Else 'NOT WORKing
Return False
End Select
Now you can set the column alignment to right using:
so, as the given example says:
ListBox1.ColumnAlignmentAt(1) = DesktopListBox.Alignments.Right
Set the aligment of Column 1 (Netto F.) to Right.
Same can be done for other columns. The code can be added in the Opening Event.
This why you read documentation, to see what is possible. Then, much later, you want to do it and you remember and look it up again.
Well, it’s why I read it, anyway.
That’s broken. Your locale uses comma as decimal separator and Val() expects a point. For Val() “5,9” and “5,1” are just 5.
You could try localStringNumber.ToDouble() instead.
Var d As Double = 5.91
Var ls As String = d.ToString(Locale.Current) // 5,91 if your country uses comma
Var n As Double = ls.ToDouble() // 5.91 back
Break
yes, I understood that.
The problem is that sometimes, without a clue, you don’t know what to look for
But it works, I load the listBox already with the numbers with the comma, and I sort those. I tried several times and everything works, the values remain with the comma.
Should I write something like this?
Var d as double=Me.CellTextAt(row1, column ).Val
Var ls As String = d.ToString(Locale.Current) // if your country uses comma
Var n1 As Double
Var n2 As Double
n1= ls.ToDouble() // . back
d=Me.CellTextAt(row2, column ).Val
ls= d.ToString(Locale.Current) // if your country uses comma
n2 = ls.ToDouble() // . back
Select Case column
Case 4
'messagebox "Sto ordinando per TOTALE..." + me.CellTextAt(row1, column).Val.ToString
If n1 < n2 Then
result = -1
ElseIf n1 > n2 Then
result = 1
Else
result = 0
End If
Return True
Case 6
'messagebox "Sto ordinando per TOTALE..." + me.CellTextAt(row1, column).Val.ToString
If n1 < n2 Then
result = -1
ElseIf n1 > n2 Then
result = 1
Else
result = 0
End If
Return True
Else 'NOT WORKing
Return False
End Select