DateTimePicker and DBNull
You can’t bind a Windows Forms DateTimePicker to a field
that might contain DBNull… so I did this;
public class
DBDateTimePicker:DateTimePicker
{
public
DBDateTimePicker()
{
//
// TODO: Add constructor logic
here
//
}
public object DBValue
{
get
{
if (this.Checked)
return base.Value;
else
return
System.DBNull.Value;
}
set
{
if (System.Convert.IsDBNull(value))
this.Checked=false;
else
this.Value = Convert.ToDateTime(value);
}
}
}
Then I bind to “DBValue” (instead of Value) and it appears to
work fine… if it is null, it is unchecked and disabled, otherwise it is enabled
and can be set to any normal date value... if you uncheck the box yourself, then
the data field is set to DBNull...
Not sure if it the best idea, but I can’t override Value so this
seems like a reasonable alternative… of course, I never looked around for the
"official" solution or any other possible answers, so let me know if you have a
better idea!