Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda