Sorting speed

This Line is not as such in my Example Project…

In the Timer1.Run Event i see:

For X As Integer = 0 To 10000
  
  ListBox1.AddRow DateTime.Now.SubtractInterval(0,0,System.Random.InRange(0, 150)).SQLDateTime
  
Next

in my ListBoxRicerca.Add … i write datetime.FromString(rows2.Column(“Data”).Value).SQLDate,

Immagine

and I get the YYYY-MM-DD formatting,
(disabling instructions in PaintCellText)

when I reactivate them, I get the error again.

but in PaintCellText I always get the same error. Why?

The highlighted line doesn’t do anything useful for you.
It was involved with generating a random date/time for demo purposes.

For your data,
try this:

The upload part…



dim SortVersion as string
dim DisplayVersion as string

While Not Rows.AfterLastRow
  ......  
SortVersion = rows2.Column("Data").datetimevalue.sqldate  
//for sorting .. the string should look like   "2024-10-15"

DisplayVersion = rows2.Column("Data").DateTimeValue.tostring (nil,DateTime.FormatStyles.Medium,DateTime.FormatStyles.None)
//for display,  the date will be whatever style the user prefers
//maybe “21 May 2024”  , maybe  “Dec 1, 2022”


//add the sortable row
ListBoxRicerca.AddRow(SortVersion)

//add the local format into the celltag for later display
ListBoxRicerca.celltagat(ListBoxRicerca.LastAddedRowIndex) = DisplayVersion 

rows.MoveToNextRow
..............
Wend

The PaintCellText part:

g.DrawingColor = &c000000
//no need to recalculate.. the tag already holds the right value
g.DrawText(Me.CellTagtAt(row,column), x, y, g.Width, True)

Replace it with:

ListBoxRicerca.Add … rows2.Column(“Data”).DateTimeValue.SQLDate

and if you do it because the DB Field is not a Date/DateTime Field but a VarChar, then do:

Try
ListBoxRicerca.Add … DateTime.FromString(rows2.Column(“Data”).StringValue).SQLDate
Catch...
...
End Catch

@Federico_Giannetti I think it is important that you take a closer look at the database concept and the ListBox.

You need to know what type of field is used in the database and, if it is a VarChar type, which encoding is used.
Based on this information, you load the date information into the ListBox in an alphanumerically sortable format. If the database field is not a Data/DateTime field, you need to prepare your code for incorrectly formatted data using Try…Catch statements.
So that the user sees the date in the format you want, you draw it on the screen yourself using the PaintCellText event.

1 Like

Note: in my code , celltagat needs two values - the row and column
I cannot amend it because Sascha has posted afterward

ListBoxRicerca.celltagat(ListBoxRicerca.LastAddedRowIndex, 0 )
in real code, I do not think your date is in the first column, so that 0 will change

1 Like

YES SASCHA !!! WORK FINE.
And sorting by date is so fast.

I really like the date displayed as in the image… the only thing I don’t like is that 00.00… to eliminate it can I use the ReplaceAll command in PaintCellText?

1 Like

Thanks Jeff for your intervention, I understood the code you wrote, which uses an additional column with data … at the moment however already solved with Sascha’s method! Thanks as always for the support.

Glad it works now. We have to thank @Jeff_Tullin for bringin us into the correct direction :slight_smile:

The Line:

g.DrawText(DateTime.FromString(Me.CellTextAt(row,column)).ToString(DateTime.FormatStyles.Long, DateTime.FormatStyles.None), x, y, g.Width, True)

dictates how to draw the Text.
DateTime.FromString(Me.CellTextAt(row,column)).ToString starts the conversion to a string.
Followed by (DateTime.FormatStyles.Long, DateTime.FormatStyles.None) which is saying that we want to see the Date formatted long (see here for FormatStyles) and the time to be not displayed (None).

Knowing this, you just need to check the Part which is drawing the CellText in the PaintCellText Event of the ListBox. Just make sure the 2nd parameter of the ToString function is set to DateTime.FormatStyles.None.

I’m in a hurry into the weekend and hope you can solve this last issue using the above information.

Have a nice weekend. :slight_smile:

Hi Sacha, thanks for everything, I thought I would never be able to solve the problem, but now everything works. Thanks of course also to Jeff and the whole forum.

I tried to set .none (the result is 00.00) or .short (the result is 21 july 2024 00.00)

Are you sure you are writing DateTime.FormatStyles.XXX twice? First for Date and second for Time? If you don’t write the second time, then Time uses a default setting (I think short).

Maybe there is another bug with your code or your locale?

Yes, I studied the guide a little and solved:

g.DrawText(DateTime.FromString(Me.CellTextAt(row,column)).ToString(DateTime.FormatStyles.Short, DateTime.FormatStyles.None), x, y, g.Width, True)

The code in PaintCellText and I get as date format 15/02/24. PERFECT !! And the sorting speed is fabulous, very fast compared to the sorting in RowComparision.

3 Likes