I have come up with a solution to handle a null DateTime value when Data Binding a RadDateTimePicker. I am using Winforms Q1 2011 and a BindingSource in C# .NET 3.5.
After looking at various discussions on how to handle null values with date pickers (
1,
2,
3 ,
4 ), I came across the MSDN article,
Improvements to Windows Forms Data Binding, and the
Automatic Handling of Null and DBNull Values section (
link).
Now, during control initialization I add the Binding.NullValue property (
link), and change the RadDateTimePicker's NullDate, NullText, and Value properties.
The Binding object NullValue and the RadDateTimePicker NullDate are set to equal values. So, when null is retrieved by the Binding object, it is defaulted to the same value as the RadDateTImePicker's NullDate. When the user deletes the value on the form, the control converts it into the NullDate and passes that to the Binding object. The Binding object converts it from a date to Null and passes it to the DataSource.
Todays date is selected and displayed in the RadDateTimePicker by off setting the NullDate 1 second compared to the Value. The Data Binding will still set the correct date in the control even though the Value property is being set.
The code below is from the InitializeComponent() of a detail form that loads one record. The radDateTimePicker is bound to a BindingSource with a strongly typed Dataset connected to SQL Server. I have made changes by copying the default line and modifying it on the line below.
//
// radDateTimePicker1
//
this
.radDateTimePicker1.Culture =
new
System.Globalization.CultureInfo(
"en-CA"
);
//this.radDateTimePicker1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.tblClientBindingSource, "ClientSinceDate", true));
this
.radDateTimePicker1.DataBindings.Add(
new
System.Windows.Forms.Binding(
"Value"
,
this
.tblClientBindingSource,
"ClientSinceDate"
,
true
, System.Windows.Forms.DataSourceUpdateMode.OnValidation, System.DateTime.Now.Date.AddSeconds(1),
""
));
this
.radDateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Long;
this
.radDateTimePicker1.Location =
new
System.Drawing.Point(491, 160);
this
.radDateTimePicker1.MaxDate =
new
System.DateTime(9998, 12, 31, 0, 0, 0, 0);
this
.radDateTimePicker1.MinDate =
new
System.DateTime(1753, 1, 1, 0, 0, 0, 0);
this
.radDateTimePicker1.Name =
"radDateTimePicker1"
;
//this.radDateTimePicker1.NullDate = new System.DateTime(2006, 1, 1, 0, 0, 0, 0);
this
.radDateTimePicker1.NullDate = System.DateTime.Now.Date.AddSeconds(1);
this
.radDateTimePicker1.NullText =
""
;
this
.radDateTimePicker1.Size =
new
System.Drawing.Size(200, 20);
this
.radDateTimePicker1.TabIndex = 71;
this
.radDateTimePicker1.TabStop =
false
;
this
.radDateTimePicker1.Text =
"radDateTimePicker1"
;
//this.radDateTimePicker1.Value = new System.DateTime(2011, 8, 3, 11, 19, 57, 978);
this
.radDateTimePicker1.Value = System.DateTime.Now.Date;