Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am on demoralized!
I switched to Sqlite with my windows forms c #, great.
Finally, knowing that there is no date format, I opted for text in dd / MM / yyyy format.
The concern now is that my Datagridview is not sorting the dates correctly.
There are a lot of topics on the web, but I can't ...

I need a little help please to find an answer….

Is it possible to sort a column by code:

dd, then MM, then YYYY ignoring the slash (/)


C#
private void Datagridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           
                if (e.ColumnIndex == Datagridview1.Columns[1].Index)
                {
                     // ici le code
                }

        }

thank you,


What I have tried:

I changed the REAL, INTEGER, TEXT, NUMERIC formats but nothing helps.

Even with this example I don't understand ..

sortablebindinglist.zip.
Posted
Updated 8-Mar-20 8:26am
v2

Use the SQLite Query Language: Date And Time Functions[^] to convert your strings, and pass proper DateTime vaues to the DataGridView.
 
Share this answer
 
Comments
LSB71 8-Mar-20 14:34pm    
Hello,
Thank you,
Indeed it works in the yyyy-MM-dd format and not in the dd / MM / yyyy format.
So with the code below it works ...
and I modify in Datagriview like this:
Datagridview1.Columns [1] .DefaultCellStyle.Format = "dd / MM / yyyy";
and it's great!
Hide Copy Code
if (e.ColumnIndex == Datagridview1.Columns[1].Index)
{
if (e.Value != null)
{

e.Value = DateTime.Parse(e.Value.ToString()).ToUniversalTime();

}
}
At a guess, you are storing your dates wrong.
If you store dates as DATE or DATETIME in your DB, then they will get "fetched" into the DGV as .NET DateTime values - and those will sort correctly at all times, regardless of the format in which they are displayed to the user.

But ... if you store them in a VARCHAR or NVARCHAR field, then they will be imported as strings and string comparisons are different: they are always resolved by the first different character between the two strings being compared - no other data is involved at all.

So check your DB, and change your table design to store data in the most appropriate format - numbers in numeric fields, dates and times in DATE or DATETIME columns. Fix your DB input code to validate date based data and pass it to the DB via a parameterised query as a DateTime value.
And then it'll all just work.
 
Share this answer
 
Comments
LSB71 8-Mar-20 11:56am    
Hello,
Not much choice alas!
Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

NULL. The value is a NULL value.
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
BLOB. The value is a blob of data, stored exactly as it was input.
https://www.sqlite.org/datatype3.html
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900