Date Formatting in Wpf Datagrid

Date formatting in WPF datagrid

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.

How do I change the date format when editing a date in a WPF Datagrid

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}"/>

Change the Date Format of existing DataGridColumn in WPF

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>

StringFormat doesn't format date (string data type) inside Data Grid in WPF

StringFormats are not applied to string properties.

You should either change the type of the source property to DateTime, return an already formatted string from the TransactionType property, or add a new property that returns a formatted date and bind to this one:

public string FormattedtTransactionTime
{
get
{
DateTime dt;
if (!string.IsNullOrEmpty(TransactionTime)
&& DateTime.TryParseExact(TransactionTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
return dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
return null;
}
}

Date formatting WPF datagrid

The "date" Sql type ends up being a DateTime in your dataset, which is why you're getting the time as well. You'll need to define columns and specify formatting on your datagrid.

EDIT: I was mistaken about not being able to format this when auto generating columns. As the other answer here mentions, this question: Need to format dates in dynamically built WPF DataGrid addresses some ways to accomplish this.

How to format my bound wpf datagrid text column to not show new DateTimes?

Converter:

class DateTimeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is DateTime)
{
var test = (DateTime)value;
if (test == DateTime.MinValue)
{
return "None";
}
var date = test.ToString("MM/dd/yyyy hh:mm");
return (date);
}

return string.Empty;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

XAML:

<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Resources>
<stackOverflow:DateTimeConverter x:Key="DateTimeConverter"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding LastRunTime, Converter={StaticResource DateTimeConverter}}" Header="Last Run Time" />
</DataGrid.Columns>
</DataGrid>

WPF datagrid date column with system custom short date format

Create a converter and use it in the binding at the datetime column. A class that inherits IValueConverter. Receives a DateTime and returns a String.



Related Topics



Leave a reply



Submit