Reputation: 13
If the entry in my SQL table is 09:00:00 my cells either show up as something like 09:00:00 or if i throw in a string format like this:
<DataGridTextColumn Binding="{Binding Path=tin, StringFormat=\{0:hh\\:mm\}}" Header="Time In:"/>
I get 9:00. How do i make it formatted for a short time string like "09:00 AM"?
I have tried using StringFormat=t and it makes the cell blank. Any use of "t" and the cell goes blank. I am really in a pickle here.
The DataGrid is populated with a dataset named displayGrid. entryGrid is the DataGrid object.
entryGrid.ItemsSource = displayGrid.Tables[0].DefaultView;
What I am looking for here is what to throw in the XAML to format this correctly. I need to go from 09:00:00 in my SQL 2008 Table to 09:00 AM in my datagrid. I am using C#.Net and XAML in VS 2012. I am super new to coding so I am sure it is a simple mistake or syntax error. Thank you for any help you can give.
EDIT:
I have even gone back, tried autogenerating the columns with the following code to format the cell:
private void ResultsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(DateTime))
{
DataGridTextColumn dataGridTextColumn = e.Column as DataGridTextColumn;
if (dataGridTextColumn.Header.ToString().Equals("wkdate"))
{
dataGridTextColumn.Binding.StringFormat = "{0:d}";
}
if(dataGridTextColumn.Header.ToString().Equals("tin"))
{
dataGridTextColumn.Binding.StringFormat = "{0:t}";
}
}
}
works for the date but not the time! This is driving me bonkers. Thanks in advance!
Upvotes: 1
Views: 1568
Reputation: 1023
As per your format, you could update your existing XAML Code and write as below:
<DataGridTextColumn Binding="{Binding Path=tin, StringFormat=\{0:hh:mm tt\}}" Header="Time In:"/>
Hope this helps
Alternatively, Could you could you the below. I am sure this is working as i have used it in many project.
CS File Code:
[ValueConversion(typeof(DateTime), typeof(string))]
public class StringTime : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || value.ToString() == "")
{
return "";
}
String str = ((DateTime)value).ToString("hh:mm tt");
return str;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || value.ToString() == "")
{
return value;
}
DateTime dtr = DateTime.MinValue.Date;
dtr = dtr.AddYears(1899);
if (value == null)
return dtr;
dtr = dtr.AddHours(double.Parse((value.ToString().Substring(0, 2))));
dtr = dtr.AddMinutes(double.Parse((value.ToString().Substring(3, 2))));
if(value.ToString().Substring(7, 2) == "PM")
dtr = dtr.AddHours(12);
return dtr;
}
#endregion
}
XAML File:
<Page.Resources>
<custom:StringTime x:Key="StringTime" />
</Page.Resources>
DataGrid Column Code:
<DataGridTextColumn Binding="{Binding Path=tin, Converter={StaticResource StringTime}}" Header="Time In:"/>
Let me know if you have any issues.
Upvotes: 1