Need to format dates in dynamically built WPF DataGrid

We bind an unknown result set to the WPF DataGrid at run time. Some of our columns will contain DateTime values, and we need to format these time fields correctly. Without knowing which columns will be DateTime fields at design time, how can we format the columns at run time?

We use the DataTable DefaultView to bind to the WPF DataGrid.

+28
datetime format wpf datagrid
May 11 '09 at 15:38
source share
8 answers

What about

<wpfToolkit:DataGridTextColumn Header="Fecha Entrada" Width="110" Binding="{Binding EnterDate,StringFormat={}\{0:dd/MM/yyyy hh:mm\}}" IsReadOnly="True" /> 

I think this is better than writing large snippets of code

+48
Aug 21 '09 at 21:04
source share

I figured out how to do this in code ... I hope there is a way to simulate this in XAML. (Please write if you find a working XAML sample.)

To accomplish this in code, add an event handler for the Grid AutoGeneratingColumn event, for example:

 private void ResultsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyType == typeof(DateTime)) { DataGridTextColumn dataGridTextColumn = e.Column as DataGridTextColumn; if (dataGridTextColumn != null) { dataGridTextColumn.Binding.StringFormat = "{0:d}"; } } } 
+29
May 11 '09 at 20:51
source share

Hey, you can set the locale culture information in the WPF form constructor as

this.Language = XmlLanguage.GetLanguage (CultureInfo.CurrentCulture.IetfLanguageTag);

Or you can enable xml xml markup : lang = "en-GB" in the window title markup

+17
Dec 03 '09 at 8:21
source share
 <DataGridTextColumn Header="Last update" Width="110" IsReadOnly="True" Binding="{Binding Path=Contact.TimeUpdate, StringFormat={}\{0:dd/MM/yyyy hh:mm\}, Mode=OneWay}" /> 
+8
Sep 01 '11 at 8:14
source share

I would use a DataTemplate with a DataType Date or DateTime (depending on how it will go through). Put a TextBlock in a DataTemplate with a StringFormat in a binding.

Something like this should work (unverified)

 <DataTemplate DataType="{x:Type DateTime}"> <TextBlock Text="{Binding StringFormat={0:d}}" /> </DataTemplate> 

Or, if you want it to be applied only to the grid

 <wpfToolkit:DataGrid> <wpfToolkit:DataGrid.Resources> <DataTemplate DataType="{x:Type DateTime}"> <TextBlock Text="{Binding StringFormat={0:d}}" /> </DataTemplate> </wpfToolkit:DataGrid.Resources> ... </wpfToolkit:DataGrid> 
+7
May 11 '09 at 4:59 pm
source share

dataGridTextColumn.Binding.StringFormat = "{0: dd / MM / yyyy}";

worked beautifully

+3
Jun 09 '11 at 20:32
source share

I run this way. his work is completed.

 <TextBlock Text="{Binding Date_start, StringFormat=\{0:dd-MM-yyyy\}, Mode=OneWay}" /> 
+2
Feb 26 '15 at 10:23
source share

FarrEver's answer, May 11th, is good, but it doesn't work for me. I still get American million / dd / yyyy instead of my German dd / mm / yyyy. Therefore, I suggest finding the regional settings of the computer and using it in StringFormat

  Private Sub DGrid_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) If e.PropertyType Is GetType(DateTime) Then Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn) If dataGridTextColumn IsNot Nothing Then Dim ShortDatePattern As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern dataGridTextColumn.Binding.StringFormat = "{0:" + ShortDatePattern + "}" '"{0:dd/MM/yyyy}" End If End If End Sub 

see also: my blog

0
Sep 02 '09 at 12:59
source share



All Articles