Reputation: 21
I'm trying to take my stored ISO8061 DateTime (yyyy/MM/dd HH:mm:ss) and convert it to an en-US short date (MM/dd/yyyy) to be presented in my datagrid. I've tried using stringformat while binding as well as creating a converter to do the job.
In the below examples: cal_date is a data table column filled via a data adapter from a SQLite database.
Here's a snippet of the model:
public DataTable RetrieveToolRoster()
{
string db_command = "SELECT [id], [description], [serial], [model], [manufacturer], [location], [cal_interval], " +
"[cal_date], [cal_due], [checked_out], [tool_lock] FROM inventory WHERE [cal_date] IS NOT NULL ORDER BY [id] ASC;";
DataTable tr_dataTable = new DataTable();
using (SQLiteConnection db_connection = new SQLiteConnection(Properties.Settings.Default.db_connectionstring))
{
using (SQLiteDataAdapter db_dataAdapter = new SQLiteDataAdapter(db_command, db_connection))
try
{
db_connection.Open();
db_dataAdapter.Fill(tr_dataTable);
db_connection.Close();
return tr_dataTable;
}
catch (Exception ex)
{
MessageBox.Show("Error:\r\n" + ex.Message);
return null;
}
}
}
Here are some examples of what I've tried.
Attempts to format while binding via StringFormat:
Binding="{Binding cal_date, StringFormat=MM/dd/yyyy}"/>
Binding="{Binding cal_date, StringFormat='MM/dd/yyyy'}"/>
Binding="{Binding cal_date, StringFormat=d}"/>
Binding="{Binding cal_date, ConverterCulture='en-US', StringFormat=d}"/>
Binding="{Binding cal_date, StringFormat={}{0:MM/dd/yyyy}}"/>
Binding="{Binding cal_date, StringFormat='{}{0:MM/dd/yyyy}'}"/>
Attempt to format via converter:
XAML
<DataGridTextColumn Header="Calibration Date:"
Width="*"
Binding="{Binding cal_date, Converter={StaticResource ISOtoENUS}}"/>
C#
class ISO8061toENUSConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
var dt = string.Format("{0:MM/dd/yyyy}", value);
return dt;
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
ALL result are the same, returning (yyyy/MM/dd HH:mm:ss)
I've been at this for almost two days now. I've searched and searched. I've even tried implementing code snippets from questions asked where they got it to work, but to no avail. What am I doing wrong?
Upvotes: 0
Views: 116
Reputation: 15209
Edit
If CalDate is a string then you can modify your converter using DateTime.TryParse:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
DateTime parsedDate = DateTime.MinValue;
if (DateTime.TryParse(value.ToString(), null, DateTimeStyles.RoundtripKind, out parsedDate))
{
return string.Format("{0:MM/dd/yyyy}", parsedDate);
}
}
return string.Empty;
}
Using a DateTime object
I think the converter way is correct, assuming cal_date property is a DateTime object:
private DateTime _calDate;
public DateTime CalDate
{
get { return _calDate; }
set
{
_calDate = value;
NotifyPropertyChanged();
}
}
And then change your converter to work with a DateTime object as well:
public class ISO8061toENUSConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is DateTime)
{
return string.Format("{0:MM/dd/yyyy}", value);
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Here is the binding I used:
DataGridTextColumn Header="Calibration Date:"
Width="*"
Binding="{Binding CalDate, Mode=OneWay, Converter={StaticResource ISOtoENUS}}"/>
And you should get the right value (I tested it and works for me).
Upvotes: 1