DateTimePicker null value...

1 Answer 1746 Views
DateTimePicker
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Jure asked on 21 Jun 2021, 07:41 AM

Hi.

After a date is picked I want to delete it and set the value to null on the data bound item.

The NullableValue is set to null/Nothing:

 Me.DateRadDateTimePicker.NullableValue = Nothing

The Value property is bound to an entity's nullable date field:

 Me.DateRadDateTimePicker.DataBindings.Add(New System.Windows.Forms.Binding("Value", Me.binder, "Date", True))

If I select the date text and delete it, the entity still has a 01/01/0001 date value. However, the null text correctly displays in the picker.

I've even tried to manually set the null date (in three different ways!) based on the empty text but to no avail:


Dim dtp As RadDateTimePicker = ctrl
If dtp.Text = "" Then
  dtp.NullableValue = Nothing
  dtp.DateTimePickerElement.SetToNullValue()
  dtp.Value = Nothing
End If

 

What am I missing?

1 Answer, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 23 Jun 2021, 08:44 AM

Hello, Jure,

The provided code snippet demonstrating the DataBindings that you add is greatly appreciated. I have noticed that you bound the Value property. This is why you are seeing the 01/01/0001 date when deleting the value from the datetimepicker.

Note, NullableValue is the same as the Value property, but the NullableValue property is of type Nullable DateTime and can be null. More information and example is demonstrated here: https://docs.telerik.com/devtools/winforms/controls/editors/datetimepicker/raddatetimepicker-properties 

In your case, you should bind the NullableValue property in order to update correctly the data object. Please refer to the following code snippet:

Public Partial Class RadForm1
    Inherits Telerik.WinControls.UI.RadForm

    Private obj As MyObject = New MyObject(Nothing)

    Public Sub New()
        InitializeComponent()
        Me.radDateTimePicker1.DataBindings.Add(New Binding("NullableValue", obj, "EndTime", True, DataSourceUpdateMode.OnPropertyChanged))
    End Sub

    Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim objValue = obj.EndTime
    End Sub
End Class

Public Class MyObject
    Public Sub New(ByVal _endTime As DateTime?)
        Me._endTime = _endTime
    End Sub

    Private _endTime As DateTime?

    Public Property EndTime As DateTime?
        Get
            Return _endTime
        End Get
        Set(ByVal value As DateTime?)
            _endTime = value
        End Set
    End Property
End Class

I hope this information helps. In case you have other questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jure
Top achievements
Rank 2
Iron
Iron
Iron
commented on 23 Jun 2021, 09:25 AM

Excellent! I knew it must be something simple. Thank you Nadya.
Tags
DateTimePicker
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or