Chetan S
Chetan S

Reputation: 945

Set default value of RadDatePicker to NULL when date is not set

I'm binding the RadDatePicker to a viewModel dateTime property. when the property is null in database i get 01/01/0001. How to i change this behavior to just leave the date field blank.. example:

In database ===> experiation_date : NULL In View(GUI) ===> experation_date : 01/01/0001

Expected Result ===> experation_date : {Blank}

Upvotes: 0

Views: 21804

Answers (3)

Stanley
Stanley

Reputation: 5491

It is not always possible to change the Data Type from DateTime to DateTime? as a DateTime object might in fact be required. (A typical scenario is when your control is bound to a database object with a DateTime type but you are busy creating a new record in your control and so the value is still null at that point.) In such a case, it is possible to write a Value Converter as Paulo does in the following link: http://www.telerik.com/forums/default-date-value-01-01-001 (See his code for the EditDateConverter. It works just as-is...) I am pasting his code here for reference and in case that link stops working in future:

public class EditDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value.GetType().FullName == "System.DateTime")
        {
            var dt2 = (DateTime)value;
            if (dt2 != DateTime.MinValue)
                return value;
        }
        DateTime dt;
        if (!DateTime.TryParse(value as string, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt))
            return dt != DateTime.MinValue ? value : null;
        return dt;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null && targetType.FullName == "System.DateTime")
            return DateTime.MinValue;
        return value;
    }
}

To use it in a binding for your RadDatePicker, simply add the converter as a resource in your XAML and then add the converter to your binding.

In your XAML namespace declarations you have to add the namespace of your converter, for example:

xmlns:converters="clr-namespace:MyNamespace.Converters"

And then in the Resources section of your XAML you would have:

<UserControl.Resources>
    <converters:EditDateConverter x:Name="NullableDate"/>
</UserControl.Resources>

Then you can simply add the following line to your binding on the control:

Converter={StaticResource NullableDate}

If you now have a null date the control will display the watermark (e.g. "Enter Date") rather than 01/01/0001.

Upvotes: 1

Chetan S
Chetan S

Reputation: 945

If you are bind not nullable datetime variable i.e (DateTime) to control then it will use default 01/01/001 date instead of Empty.... So if you want the default value as a Empty then just change the datatype of your variable which is bound to control to (DateTime?).....

DateTime ===> DateTime?

It works fine...

Thank you.!!!

Upvotes: 0

Rumplin
Rumplin

Reputation: 2768

Set the SelectedDate to null (and not DBNull):

RadDatePicker1.SelectedDate = null;

and in xaml set DateTimeWatermarkContent to the empty string:

<telerik:RadDatePicker x:Name="RadDatePicker1" DateTimeWatermarkContent="" />

Upvotes: 1

Related Questions