How to show just the date and not date and time in a WPF datagrid?

让人想犯罪 __ 提交于 2021-02-18 22:08:53

问题


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?


回答1:


Try this

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

more info on string formattings here




回答2:


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)



来源:https://stackoverflow.com/questions/32131208/how-to-show-just-the-date-and-not-date-and-time-in-a-wpf-datagrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!