Handle Unsupported Values
There are cases where an editor might not support some values from your data source. A good example of this is if when you have a DateTime field in your data and this field contains a record with DBNull value. RadDataEntry will resolve the field type to DateTime and will create a RadDateTimePicker editor for it, however, since it does not support displaying DBNull value as data, it does not know how to handle it and the simple data binding will fail.
To handle this case, you need to tell the RadDateTimePicker, how to interpret this value. This can be done by using the Format event of the binding.
The following example demonstrates how to do that:
1. In the BindingCreating event of the control you can tell which property to bind the field to. Here you should bind the "DateTime" field from the data source to the "NullableValue" property of RadDateTimePicker (as it supports null values).
Map Nullable Property
void radDataEntry1_BindingCreating(object sender, BindingCreatingEventArgs e)
{
if (e.DataMember == "DateTime")
{
e.PropertyName = "NullableValue";
}
}
Private Sub radDataEntry1_BindingCreating(sender As Object, e As BindingCreatingEventArgs)
If e.DataMember = "DateTime" Then
e.PropertyName = "NullableValue"
End If
End Sub
2. Then in the BindingCreated event you will enable the binding formatting and will subscribe to its Format event.
Enable Formatting
void radDataEntry1_BindingCreated(object sender, BindingCreatedEventArgs e)
{
if (e.DataMember == "DateTime")
{
e.Binding.FormattingEnabled = true;
e.Binding.Format += Binding_Format;
}
}
Private Sub radDataEntry1_BindingCreated(sender As Object, e As BindingCreatedEventArgs)
If e.DataMember = "DateTime" Then
e.Binding.FormattingEnabled = True
AddHandler e.Binding.Format, AddressOf Binding_Format
End If
End Sub
3. At the end we just need to interpret the DBNull values as null values:
Evaluate DBNull
void Binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value.Equals(DBNull.Value))
{
e.Value = null;
}
}
Private Sub Binding_Format(sender As Object, e As ConvertEventArgs)
If e.Value.Equals(DBNull.Value) Then
e.Value = Nothing
End If
End Sub