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!
As per your format, you could update your existing XAML Code and write as below:
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:
XAML File:
Let me know if you have any issues.