
Joel Kraft
Top achievements
Rank 2
Joel Kraft
asked on 27 Jun 2008, 03:48 PM
I'm working on an inventory form and one of the (optional) fields is ReplacementDate. Obviously, for equipment that is still in service, there won't be a value for the ReplacementDate. Is there a way for either the RadMaskedTextBox (with a DateTime mask) or RadDateTime picker to handle a nullable DateTime natively, or do I have to use a numeric mask and handle the Validating event?
7 Answers, 1 is accepted
0
Hello Joel Kraft,
Thank you for the question.
RadDateTimePicker has NullDate support. When you set its Value to its NullDate (NullDate by default is equal to the MinDate), the text you will see in RadDateTimePicker would be set to the NullText. NullText by default is empty string. Please refer to the code snippets below for customizations in this scenario:
Sincerely yours,
Nikolay
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Thank you for the question.
RadDateTimePicker has NullDate support. When you set its Value to its NullDate (NullDate by default is equal to the MinDate), the text you will see in RadDateTimePicker would be set to the NullText. NullText by default is empty string. Please refer to the code snippets below for customizations in this scenario:
- Set the NullDate to a different date than the MinDate:
this.radDateTimePicker1.DateTimePickerElement.NullDate = new DateTime(2000, 01, 01); - In order to set the value to NullDate, call the SetToNullValue method:
this.radDateTimePicker1.DateTimePickerElement.SetToNullValue(); - You can also change the NullText which will appear, when the Value is set to the NullDate value:
this.radDateTimePicker1.NullText = "No date selected";
Sincerely yours,
Nikolay
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0

Stargazer
Top achievements
Rank 2
answered on 11 Jul 2008, 10:28 AM
Hi!
This doesn't quite cut it for me... I'm binding a Nullable DateTime property to my DateTimePicker and i need it to be null so my property is also null, when there is no value coming form the Database and, if the user didn't put a value there, I need a null in the property to make sure no date get's into my database.
As I understand, I can not do his with direct databinding. I will need to derive a control from RadDatePicker and make it nullable (not sure how to do that yet, as all my tries failed to work properly) or else, need to do this manualy, without bindings...
Am I right, or is there any other way escaping me?
Thanks!
This doesn't quite cut it for me... I'm binding a Nullable DateTime property to my DateTimePicker and i need it to be null so my property is also null, when there is no value coming form the Database and, if the user didn't put a value there, I need a null in the property to make sure no date get's into my database.
As I understand, I can not do his with direct databinding. I will need to derive a control from RadDatePicker and make it nullable (not sure how to do that yet, as all my tries failed to work properly) or else, need to do this manualy, without bindings...
Am I right, or is there any other way escaping me?
Thanks!
0
Hello Stargazer,
You cannot directly bind the Value property of RadDateTimePicker in order to achieve your scenario. However, you can use the NullDate property and check whether the date entry is equal to it. If they are equal, you should put the null value in your database manually.
If you have additional questions, feel free to contact me.
Regards,
Nikolay
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
You cannot directly bind the Value property of RadDateTimePicker in order to achieve your scenario. However, you can use the NullDate property and check whether the date entry is equal to it. If they are equal, you should put the null value in your database manually.
If you have additional questions, feel free to contact me.
Regards,
Nikolay
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0

Joel Kraft
Top achievements
Rank 2
answered on 18 Dec 2008, 10:38 PM
Finally, after WAY too much time working on it, I've wrapped the RadDateTimePicker to bind directly to a DateTime?. I created a new component which inherits from RadDateTimePicker and adds the NullableValue property, which supports (or at least seems to, I haven't had a chance to test it much) two-way binding with nullable DateTimes. It also adds the NullableValueChanged and CheckedChanged events. Parts of this were interesting learning experiences, parts of it were really frustrating (like the lack of a CheckedChanged event on a control with a checkbox). If others have refinements to the code, feel free to post them here.
I've designed the control so that if the checkbox is unchecked, NullableValue is null (which makes sense, since unchecking the box disables date/time picking). If the checkbox is checked, NullableValue is the value of the date/time picker.
Since I've seen a number of requests for this functionality, Telerik may want to strongly consider updating this in future releases.
I've designed the control so that if the checkbox is unchecked, NullableValue is null (which makes sense, since unchecking the box disables date/time picking). If the checkbox is checked, NullableValue is the value of the date/time picker.
Since I've seen a number of requests for this functionality, Telerik may want to strongly consider updating this in future releases.
using System; |
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Diagnostics; |
using System.Linq; |
using System.Text; |
using Telerik.WinControls.UI; |
using System.Windows.Forms; |
using Telerik.WinControls.Themes.Design; |
namespace ThisTookWayTooLong |
{ |
[RadThemeDesignerData(typeof(RadDateTimePickerDesignTimeData))] |
[Description("Enables the user to select a date and time, and display the date and time in a specified format, allows for null DateTimes.")] |
[DefaultBindingProperty("NullableValue"), DefaultEvent("NullableValueChanged"), DefaultProperty("NullableValue")] |
public partial class RadNullableDTP : RadDateTimePicker, INotifyPropertyChanged |
{ |
#region Constructors |
public RadNullableDTP() : this(null) |
{ } |
public RadNullableDTP(IContainer container) |
{ |
if(container != null) |
container.Add(this); |
InitializeComponent(); |
this.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged; |
// Show the checkbox to allow user to toggle null/non-null values. |
ShowCheckBox = true; |
// Setting some visual defaults for how we most frequently use the component. |
Format = System.Windows.Forms.DateTimePickerFormat.Short; |
// This event is the only one I could find that gets fired when the checkbox is un/checked. |
base.DateTimePickerElement.PropertyChanged += new |
PropertyChangedEventHandler(DateTimePickerElement_PropertyChanged); |
// Subscribe to the base control's ValueChanged event |
base.ValueChanged += new EventHandler(RadNullableDTP_ValueChanged); |
} |
/// <summary> |
/// Updates the NullableValue when the base control's Value changes. |
/// </summary> |
void RadNullableDTP_ValueChanged(object sender, EventArgs e) |
{ |
OnNullableValueChanged(e); |
} |
#endregion |
#region Events |
[Description("Occurs when the state of the checkbox is toggled"), Category("Action")] |
public event EventHandler CheckedChanged; |
void DateTimePickerElement_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
if (e.PropertyName == "Checked") |
{ |
OnCheckedChanged(EventArgs.Empty); |
OnNullableValueChanged(EventArgs.Empty); |
} |
} |
private void OnCheckedChanged(EventArgs e) |
{ |
if (CheckedChanged != null) |
CheckedChanged(this, e); |
} |
[Description("Occurs when the NullableValue property of the control has changed"), Category("Action")] |
public event EventHandler NullableValueChanged; |
private void OnNullableValueChanged(EventArgs e) |
{ |
// Update any fields bound to NullableValue. |
foreach (Binding b in DataBindings) |
{ |
if (b.PropertyName == "NullableValue") |
b.WriteValue(); |
} |
// Fire the updated event |
if (NullableValueChanged != null) |
NullableValueChanged(this, e); |
} |
#endregion |
#region NullableValue |
[Bindable(true), RefreshProperties(RefreshProperties.All), Description("Gets or sets the date/time value assigned to the control."), Category("Behavior")] |
public DateTime? NullableValue |
{ |
get |
{ |
if (base.Value == NullDate || (this.ShowCheckBox && !this.Checked)) |
return null; |
return base.Value; |
} |
set |
{ |
if (value == null) |
{ |
// The following line will set the value to the NullDate of the control. |
// I'm not fond of using NullDate, but you can uncomment it if you like it. |
//base.Value = NullDate; |
this.Checked = false; |
} |
else |
{ |
base.Value = (DateTime)value; |
this.Checked = true; |
} |
OnNullableValueChanged(EventArgs.Empty); |
} |
} |
#endregion |
} |
} |
0
Hi Stargazer,
Thank you very much about your interesting post here.
We will take a look at your code very deeply and will think about improving the NullDate support of RadDateTimePicker. There is no CheckedChanged event because you have a PropertyChanged event and in its handler you can get the necessary information, like the name of the changed property.
We also liked you approach with the checkbox when you are checking/unchecking it . We will think about putting some built in code about this functionality for the next release.
If you have any other interesting ideas please feel free to contact us.
All the best,
Boyko Markov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Thank you very much about your interesting post here.
We will take a look at your code very deeply and will think about improving the NullDate support of RadDateTimePicker. There is no CheckedChanged event because you have a PropertyChanged event and in its handler you can get the necessary information, like the name of the changed property.
We also liked you approach with the checkbox when you are checking/unchecking it . We will think about putting some built in code about this functionality for the next release.
If you have any other interesting ideas please feel free to contact us.
All the best,
Boyko Markov
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0

Stargazer
Top achievements
Rank 2
answered on 19 Dec 2008, 03:51 PM
Hello Boyko!
In fact, the code is not mine. The code is of Joel Kraft, and it is indeed very cool coding. ;)
Kudos for him!
In fact, the code is not mine. The code is of Joel Kraft, and it is indeed very cool coding. ;)
Kudos for him!
0

Elliott
Top achievements
Rank 2
answered on 22 Sep 2010, 08:48 PM
my problem was a lot simpler and this thread solved it
when I selected the MinDate the date value would disappear
another solution would be to set the NullDate back by one day
null date is not applicable for this application - there is a default date mid way in a range to use instead
when I selected the MinDate the date value would disappear
another solution would be to set the NullDate back by one day
null date is not applicable for this application - there is a default date mid way in a range to use instead