Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!