Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a Null or default DateTime value as "blank" or NA in a WPF datagrid

I have a datagrid that is bound to an ObservableCollection of custom objects. One of the columns is bound to a DateTime property, and many of the objects have minimum DateTime values.

In my DataGrid, here is my code for the DateTime column

<DataGridTextColumn Binding="{Binding Path=StartTime, StringFormat={}{0:MM/dd/yyyy hh:mm:ss tt}}" Header="Start Time" Foreground="Black" Width="2*"/>

How can I apply a style with a datatemplate or something that can display "-" or "NA" or something if the date is "1/1/1753 12:00:00 AM" or "1/1/0001 12:00:00 AM"?

like image 267
Dbloom Avatar asked Jan 22 '15 01:01

Dbloom


1 Answers

Use Converter, for example:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value != null && value is DateTime && (DateTime)value < new DateTime(2, 1, 1))
    {
        return "NA";
    }
    else
        return value;
}

You should set a minimum/default DateTime value or a list and compare with DateTime column, return the result what you want.

like image 190
Rang Avatar answered Sep 28 '22 04:09

Rang