Home > Software design >  How do I change the date format when editing a date in a WPF Datagrid
How do I change the date format when editing a date in a WPF Datagrid

Time:04-24

Whenever I change a date in my WPF datagrid, it try's to convert my input to MM-dd-yyyy.

XAML

    <Grid>
        <DataGrid x:Name="datagrid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created" Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

When the data is displayed in is in the correct format, dd/MM/yyyy, however when it try to change to something like 04/08/2022 then it displays it as 08/04/2022.

And if I type in 25/08/2022, then it does not accept the date, as there is no 25th month.

I have checked the language / region settings and can confirm they are in the right format. I have also tried adding... System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");

I can add the CellEditEnding event and change the entered value to the correct format, but it seems there should be a much simpler way.

CodePudding user response:

Try to set the ConverterCulture property of the binding:

<DataGridTextColumn x:Name="DGC_CreatedTime" Header="Created"
                    Binding="{Binding CreatedTime, StringFormat=dd/MM/yyyy, 
                        ConverterCulture=en-NZ}"/>
  • Related