Setting a Display format for an automatically generated column in a Datagrid

Jun 21, 2009 at 1:41 PM

I'm retrieving data from SQL Server CE into a DataTable and setting the grid ItemsSource to the DefaultView of the DataTable.

How do I change the format by which dates (for example) are displayed?

The default format is 'm/d/yyyy hh:mm:ss am/pm', eg '6/21/2009 12:00:00AM' and I want to display '21 Jun 2009'.


Jun 21, 2009 at 2:16 PM

I found the answer to this one for myself. Look for the column in the data grid and then cast the DataGridColumn to a DataGridTextColumn and then you can adjust the Binding.StringFormat to give the output desired.

DataTable t;

DataGrid g;

DataGridTextColumn tc = (DataGridTextColumn) g.columns[t.columns["datecol"].ordinal];

tc.Binding.StringFormat = "dd MMM yyyy";