Bruno Yu - MSFT
Gpg,
To avoid this behavior, trap the data before it is displayed in the control, and then change the data so that it is valid.
For example, the following code traps the DBNull value and changes the value to a date value of January 1, 1800.
This code uses event handlers with the DateTimePicker control to monitor when the value changes. If the value is null, the value is changed to January 1, 1800.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDataSet As New DataSet()
Dim MyTable As New Data.DataTable("dsTable")
Dim MyColumn As New DataColumn("HireDate")
MyDataSet.Tables.Add(MyTable)
MyColumn.AllowDBNull = True
MyColumn.DataType = GetType(System.DateTime)
MyTable.Columns.Add(MyColumn)
Dim MyData(0) As Object
MyTable.Rows.Add(MyData)
Dim MyBinding As New Binding("Value", MyDataSet.Tables("dsTable"), "HireDate")
AddHandler MyBinding.Format, AddressOf DTFormatter
AddHandler MyBinding.Parse, AddressOf DTParser
DateTimePicker1.DataBindings.Add(MyBinding)
End Sub
Private Sub DTFormatter(ByVal sender As Object, ByVal e As ConvertEventArgs)
Dim b As Binding = CType(sender, Binding)
If Not e.DesiredType Is GetType(DateTime) Then
Return
End If
If e.value.GetType Is GetType(System.DBNull) Then
e.value = CType("1/1/1800", System.DateTime)
End If
End Sub
Private Sub DTParser(ByVal sender As Object, ByVal e As ConvertEventArgs)
If Not e.DesiredType Is GetType(DateTime) Then
Return
End If
If Not e.value.GetType Is GetType(DateTime) Then
Return
End If
Dim value As String = CType(e.Value, String)
If value.Equals("1/1/1800") Then
e.value = System.DBNull.value
End If
End Sub