Nullable DateTime in PropertyGrid...

Now a lot of people hate it, but I think the PropertyGrid is cool - excellent for quick generation of edit UI.  We have shown it to customers as part of prototype build, and they have asked for its inclusion!  I was using the PropertyGrid to define a really simple editor for some data types and was working really well until I made the DateTime properties Nullable.

public virtual DateTime? EndDate
{
    get { return m_endDate; }
    set { m_endDate = value; }
}

The PropertyGrid was not displaying the editor, bit of a bummer! Although not a massive deal, I thought I was going to need to create a UITypeEditor that would cope with Nullable dates.  Fortunately if you are explicit, the built in editor supports a Nullable DateTime. 

[EditorAttribute(typeof(DateTimeEditor), typeof(UITypeEditor))]
public virtual DateTime? EndDate
{
    get { return m_endDate; }
    set { m_endDate = value; }
}

Adding the EditorAttribute specifying the DateTimeEditor to the property forces the use of the built in editor for the Nullable type.

Comments are closed