Search code examples
sql.netwpfvisual-studiodatagrid

Change the Date Format of existing DataGridColumn in WPF


I have a DataGrid in my WPF application and it's ItemsSource Property is set to a DataTable. But the problem is that the Date Column shows date in the format "mm/dd/yyyy" And want in the DateFormat "dd/mm/yyyy". So what will be the most optimum way to achieve it? Since I'm querying the table from MS Access Database I always receive the date in English format


Solution

  • I see two easy ways to set the desired date representation: setting the required culture (language) in the DataGrid and controlled auto-generation of columns.
    In the second option, you customize the columns you need in the AutoGeneratingColumn event.

    Example:

    using System;
    using System.Data;
    
    namespace DateColumnFormat
    {
        public class DatesSource
        {
            public DataTable Table { get; } = new DataTable();
    
            private static readonly Random random = new Random();
            private static readonly DateTime begin = new DateTime(1900, 1, 1);
            private static readonly DateTime end = DateTime.Today;
            private static readonly double interval = (end - begin).TotalSeconds;
    
            public DatesSource()
            {
                // Creating one column and ten rows with random dates
    
                Table.Columns.Add(new DataColumn("Dates", typeof(DateTime)));
    
                for (int i = 0; i < 10; i++)
                {
                    DataRow newRow = Table.NewRow();
    
                    newRow[0] = begin.AddSeconds(random.NextDouble() * interval);
    
                    Table.Rows.Add(newRow);
                }
    
            }
        }
    }
    

    View:

    <Window x:Class="DateColumnFormat.FormatTestWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:DateColumnFormat"
            mc:Ignorable="d"
            Title="FormatTestWindow" Height="300" Width="500">
        <FrameworkElement.DataContext>
            <local:DatesSource/>
        </FrameworkElement.DataContext>
        <UniformGrid Rows="1">
            <DataGrid ItemsSource="{Binding Table}"/>
            <DataGrid ItemsSource="{Binding Table}" Language="ru"/>
            <DataGrid ItemsSource="{Binding Table}" AutoGeneratingColumn="OnAutoGeneratingColumn"/>
        </UniformGrid>
        <x:Code>
            <![CDATA[
                private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
                {
                    if (e.PropertyName == "Dates")
                    {
                        var column = (DataGridTextColumn)e.Column;
                        var binding = (Binding)column.Binding;
                        binding.StringFormat = "dd-MMMM-yyyy";
                        binding.ConverterCulture = System.Globalization.CultureInfo.GetCultureInfo("de-De");
                    }
                }
            ]]>
        </x:Code>
    </Window>