9

I have a WPF datagrid with a binding to a class object that looks like the following:

<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate }" />

The property in the class is quite simple, as follows:

public DateTime OrderDate { get; set; }

When I'm populating this property I'm just sending DateTime.Date (date only, not time):

.Select(
    r =>
    new Customer
        {
            Persona = r.Persona,
            CustomerName = r.CustomerName,
            Email = r.Email,
            Organization = r.Organization,
            PhoneNumber = r.PhoneNumber,
            Street = r.Street,
            Suburb = r.Suburb,
            Postcode = r.Postcode,
            OrderDate = r.OrderDate.Date
        }))

Unfortunately this doesn't reflect in my WPF datagrid as that's showing the date, but then it's also showing the time (everywhere) as 12:00:00AM. How should I approach just having my WPF datagrid show the date only?

2 Answers 2

15

Try this

<DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate , StringFormat=d}" />

more info on string formattings here

0
0

Alternately you can do it in the code:

((DataGridTextColumn)YourGridName.Columns[0]).Binding.StringFormat = "d";

(Credit: I got his information from Mark Feldman's answer to this question: Date formatting in WPF datagrid)

Not the answer you're looking for? Browse other questions tagged or ask your own question.