Date formatting in WPF datagrid

Harald picture Harald · Nov 2, 2011 · Viewed 105k times · Source

I want to change is the date column from a format "DD/MM/YYYY HH:MM:SS" to "DD.MM.YYYY".

  <DataGrid Name="dgBuchung" AutoGenerateColumns="True" 
            ItemsSource="{Binding}" Grid.ColumnSpan="3" >
  <ab:DataGridTextColumn Header="Fecha Entrada" Width="110"  
       Binding="{Binding date, StringFormat={}{0:dd/MM/yyyy}}" IsReadOnly="True" />
                        
</DataGrid>
                                      

Unfortunately that code throws an XMLParseException.

First of all, is this way of solution possible while using AutoGenerateColumns? If no, how else can I try to handle this?

If yes, what is the problem with the code above?

Answer

icebat picture icebat · Nov 2, 2011

Don`t forget to use DataGrid.Columns, all columns must be inside that collection. In my project I format date a little bit differently:

<tk:DataGrid>
    <tk:DataGrid.Columns>
        <tk:DataGridTextColumn Binding="{Binding StartDate, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" />
    </tk:DataGrid.Columns>
</tk:DataGrid>

With AutoGenerateColumns you won`t be able to contol formatting as DataGird will add its own columns.