Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagrid column which is bound to a string property called BDT

What I have tried:

C#
<DataGridTextColumn
Header="BDate"
Binding="{Binding Path=BDT, StringFormat='dd-MM-yyyy', Mode=OneWay}"
IsReadOnly="True" />


Now, the actual data in the database is in the format yyyy-MM-dd and it is of type string as well but I want to display it in the format dd-MM-yyyy. However, the above code is not doing the job, the data is still displayed in the datagrid column in yyyy-MM-dd format.

How do I fix this?
Posted
Updated 15-Oct-22 0:30am
Comments
Richard MacCutchan 15-Oct-22 5:15am    
You should store dates as proper Date or DateTime types, not strings. The only time they need to be converted to strings is when displaying them in human readable form.
Tamal Banerjee 15-Oct-22 6:27am    
I'm using SQLite database, so no option for `Date` or `DateTime` datatype

Date based formatting only works on date based values: strings are not date based, and can contain anything.

The problem is your database (or how you interpret data from your database) - you need to change it so that date based date is stored in date based fields, numerics in appropriate numeric datatypes, and so on: if you might need to sort data, do maths with data, or do anything real-world with it, you need to stroie it in appropriate datatypes. This is including displaying it since the user selected Locale should be in charge of the format used for both input and output of such data.

Change your data source: you will probably find a load of data errors or ambiguity has already crept in, and that problem will only get worse the longer you leave it!
 
Share this answer
 
Comments
Tamal Banerjee 15-Oct-22 6:32am    
I'm using SQLite database, so no option for `Date` or `DateTime` datatype.

I using the model like

private string _bDt;

public string BDt

{
 get {
 DateTime dt;
 var b = DateTime.TryParse(_bDt, out dt);
 return b ? dt.ToString("yyyy-MM-dd") : _bDt;
}

set {   SetValue(ref _bDt, value); }}
You can use the SQLite Date And Time Functions[^] to convert the display.
 
Share this answer
 

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