1

I'm using a DataTable (created in code), and I display it's DefaultView using a DataGrid. The DataTable contains a DataColumn that has DateTime as it's DataType.

The DataGrid displays dates as: 10/9/2017 12:00:00 AM. Is there a way to display the DateTime values as 10/9/2017 without changing the column's DataType to string?

0

2 Answers 2

4

Your Binding should look like this

{Binding Property, StringFormat=d}"

Full example

<DataGridTextColumn Header="Your Header" Binding="{Binding Property, StringFormat=d}" ></DataGridTextColumn>
Sign up to request clarification or add additional context in comments.

2 Comments

The DataGrid in xaml contains no column definitions, it's just bound to the DataTable's default view. Works perfectly, except for the date display.
lets c. gimme some time
2

You could handle the AutoGeneratingColumn event for the DataGrid:

private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))
    {
        e.Column = new DataGridTextColumn()
        {
            Binding = new Binding(e.PropertyName) { StringFormat = "d" }
        };
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.