DateTimePicker with nullable DateTime...

More work with nullable DateTimes!

There are oodles of articles out there discussing data binding with the DateTimePicker control attempting to address its limitations for support of nullable types.  Of course you can define your own control, or override the built in to amend the behaviour, but I wanted to see if I could use some of the .NET syntactic sugar to provide a solution that had light impact.

The idea was based on this article http://windowsclient.net/blogs/faqs/archive/2006/05/18/what-is-the-proper-way-to-bind-nullable-datetime-property-to-datetimepicker.aspx, which was obviously posted during the .NET 2 beta as it still uses INullableValue in the implementation.  The basic of this article is to use the Format and Parse events of the Binding to deal with null using the check box.  This is pretty light touch, so I thought I would wrap this in an extension to the DateTimePicker itself.  This is the extension class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace manik.Windows.Forms
{
    public static class NullableDateTimePickerExtension
    {
        public static void AddNullableBinding(this DateTimePicker picker, Binding binding)
        {
            picker.DataBindings.Add(binding);
 
            binding.Format += new ConvertEventHandler(picker.OnFormatNullableDateTimePicker);
            binding.Parse += new ConvertEventHandler(picker.OnParseNullableDateTimePicker);
        }
 
        public static void OnParseNullableDateTimePicker(this DateTimePicker picker, object sender, ConvertEventArgs e)
        {
            Binding binding = sender as Binding;
 
            if (binding != null)
            {
                DateTimePicker dtp = (binding.Control as DateTimePicker);
 
                if ((dtp != null) && (!dtp.Checked))
                    e.Value = new Nullable<DateTime>();
            }
        }
 
        public static void OnFormatNullableDateTimePicker(this DateTimePicker picker, object sender, ConvertEventArgs e)
        {
            if (e.Value == null)
            {
                Binding binding = sender as Binding;
 
                if (binding != null)
                {
                    DateTimePicker dtp = (binding.Control as DateTimePicker);
 
                    if (dtp != null)
                    {
                        dtp.Checked = false;
                        e.Value = dtp.Value;
                    }
                }
            }
        }
    }
}

The AddNullableBinding extension will take a Binding object and hook up the Parse and Format events to further extension methods on the DateTimePicker.  Usage is really simple:

Binding testDateBinding = new Binding("Value", m_dataObject, "TestDate", true);
dtTestDate.AddNullableBinding(testDateBinding);

Just create a Binding for the date object, then call the extension method (making sure you have the correct using available of course) AddNullableBinding and that's it.  Light impact...

Comments are closed