76

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?

7 Answers 7

142

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.

8
  • Ok, thank you for that! Is it possible to bind a Column to a field out of my database without having a property in my project?
    – Harald
    Nov 2, 2011 at 10:12
  • 1
    @Harald, no, you cant bind directly to database field, first you have to make or generate some object (for example, using Entity Framework). If you already have a collection of such objects, then you can bind it to DataGrid.ItemsSource property, as you did in your code.
    – icebat
    Nov 2, 2011 at 10:28
  • If StringFormat depends of Culture? For sample, En-US : dd.MM.yy , FR : dd-MM-yyyy, ES-es: dd/MM/yyyy, etc
    – Kiquenet
    Sep 17, 2014 at 8:48
  • 1
    @Kiquenet, if you have some custom format for every culture then you better move that logic to Converter or property getter. Otherwise you can look into some more options with custom date formatting
    – icebat
    Sep 17, 2014 at 8:59
  • 1
    @KonradViltersten, it's an index for string.Format that is used when StringFormat is applied.
    – icebat
    Aug 10, 2015 at 16:03
116

Very late to the party here but in case anyone else stumbles across this page...

You can do it by setting the AutoGeneratingColumn handler in XAML:

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn"  ..etc.. />

And then in code behind do something like this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(System.DateTime))
        (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
}
8
  • 3
    Great! In case you need a VB.NET version: Private Sub MyGrid_AutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs) handles MyGrid.AutoGeneratingColumn If e.PropertyType = GetType(System.DateTime) Then TryCast(e.Column, DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy" End If End Sub Sep 22, 2015 at 10:24
  • (e.Column as DataGridTextColumn).Binding.StringFormat = "d"; // use Short Date format Jan 6, 2017 at 23:08
  • Format date columns differently using e.PropertyName: if (e.PropertyName == "Open_Date") {(e.Column as DataGridTextColumn).Binding.StringFormat = "d";} Jan 6, 2017 at 23:10
  • 3
    I would use DataGridBoundColumn instead of DataGridTextColumn to be more generic.
    – JohnyL
    Nov 9, 2018 at 16:44
  • 1
    @awi2b there are a few other ways to do that, but it really depends on your circumstances. A simple method would be to use a value converter, but then a convertor by itself isn't going to know when you need to change the format, so you'd probably have to use a multiconvertor that looks at both the value and whatever is controlling the way you want to display it. Apr 10 at 10:03
18

If your bound property is DateTime, then all you need is

Binding={Property, StringFormat=d}
1
  • Worked like a charm. Was in need of formatting which doesn't interfere with system format.
    – Kshitij
    Sep 7, 2021 at 11:59
8
Binding="{Binding YourColumn ,StringFormat='yyyy-MM-dd'}"
1
  • 5
    Please add an explanation of your code, it will greatly increase the quality of the answer.
    – Nic3500
    Jul 27, 2018 at 0:12
3

first select datagrid and then go to properties find Datagrid_AutoGeneratingColumn and the double click And then use this code

   Datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                if (e.PropertyName == "Your column name")
                    (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy";
                if (e.PropertyName == "Your column name")
                    (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy";
            }

I try it it works on WPF

1
  • Although I'm using Mark Feldman for my project, your solution is very useful for those cases when you have different properties of the same type and you want different formats for each one. May 21, 2021 at 8:16
2

I know the accepted answer is quite old, but there is a way to control formatting with AutoGeneratColumns :

First create a function that will trigger when a column is generated :

<DataGrid x:Name="dataGrid" AutoGeneratedColumns="dataGrid_AutoGeneratedColumns" Margin="116,62,10,10"/>

Then check if the type of the column generated is a DateTime and just change its String format to "d" to remove the time part :

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if(YourColumn == typeof(DateTime))
            {
                e.Column.ClipboardContentBinding.StringFormat = "d";
            }
        }
1
  • 2
    That's what Mark Feldman already answered. May 21, 2021 at 8:18
2

This is a very old question, but I found a new solution, so I wrote about it.

First of all, is this way of solution possible while using AutoGenerateColumns?

Yes, that can be done with AttachedProperty as follows.

<DataGrid AutoGenerateColumns="True" 
   local:DataGridOperation.DateTimeFormatAutoGenerate="yy-MM-dd"
   ItemsSource="{Binding}" />

AttachedProperty

There are two AttachedProperty defined that allow you to specify two formats. DateTimeFormatAutoGenerate for DateTime and TimeSpanFormatAutoGenerate for TimeSpan.

class DataGridOperation
{
    public static string GetDateTimeFormatAutoGenerate(DependencyObject obj) => (string)obj.GetValue(DateTimeFormatAutoGenerateProperty);
    public static void SetDateTimeFormatAutoGenerate(DependencyObject obj, string value) => obj.SetValue(DateTimeFormatAutoGenerateProperty, value);
    public static readonly DependencyProperty DateTimeFormatAutoGenerateProperty =
        DependencyProperty.RegisterAttached("DateTimeFormatAutoGenerate", typeof(string), typeof(DataGridOperation),
            new PropertyMetadata(null, (d, e) => AddEventHandlerOnGenerating<DateTime>(d, e)));

    public static string GetTimeSpanFormatAutoGenerate(DependencyObject obj) => (string)obj.GetValue(TimeSpanFormatAutoGenerateProperty);
    public static void SetTimeSpanFormatAutoGenerate(DependencyObject obj, string value) => obj.SetValue(TimeSpanFormatAutoGenerateProperty, value);
    public static readonly DependencyProperty TimeSpanFormatAutoGenerateProperty =
        DependencyProperty.RegisterAttached("TimeSpanFormatAutoGenerate", typeof(string), typeof(DataGridOperation),
            new PropertyMetadata(null, (d, e) => AddEventHandlerOnGenerating<TimeSpan>(d, e)));

    private static void AddEventHandlerOnGenerating<T>(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!(d is DataGrid dGrid))
            return;

        if ((e.NewValue is string format))
            dGrid.AutoGeneratingColumn += (o, e) => AddFormat_OnGenerating<T>(e, format);
    }

    private static void AddFormat_OnGenerating<T>(DataGridAutoGeneratingColumnEventArgs e, string format)
    {
        if (e.PropertyType == typeof(T))
            (e.Column as DataGridTextColumn).Binding.StringFormat = format;
    }
}

How to use

View

<Window
   x:Class="DataGridAutogenerateCustom.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:DataGridAutogenerateCustom"
   Width="400" Height="250">
   <Window.DataContext>
      <local:MainWindowViewModel />
   </Window.DataContext>
   <StackPanel>
      <TextBlock Text="DEFAULT FORMAT" />
      <DataGrid ItemsSource="{Binding Dates}" />

      <TextBlock Margin="0,30,0,0" Text="CUSTOM FORMAT" />
      <DataGrid
         local:DataGridOperation.DateTimeFormatAutoGenerate="yy-MM-dd"
         local:DataGridOperation.TimeSpanFormatAutoGenerate="dd\-hh\-mm\-ss"
         ItemsSource="{Binding Dates}" />
   </StackPanel>
</Window>

ViewModel

public class MainWindowViewModel
{
    public DatePairs[] Dates { get; } = new DatePairs[]
    {
        new (){StartDate= new (2011,1,1), EndDate= new (2011,2,1) },
        new (){StartDate= new (2020,1,1), EndDate= new (2021,1,1) },
    };
}

public class DatePairs
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public TimeSpan Span => EndDate - StartDate;
}

demo_wpf_datagrid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.