1

I am looking for a datepicker like what microsoft provides, but it doesn't support null values, and since this is tied to a nullable field on a database that isn't acceptable.

I found this one, but according to the comments at the bottom of the page it has issues with binding to a database. I also have one in my project that I inherited, but it has similar issues (sometimes it shows values, sometimes it doesn't). Does anyone know of one that works?

Malfist
  • 31,179
  • 61
  • 182
  • 269
  • Someone has to have came across this problem before and solved it, probably far better than I could. I've looked and have not found one. My question is if those other people have found one that works. – Malfist May 14 '09 at 20:38

4 Answers4

4

Use a date picker to populate a textbox and if they want the field to be null, just erase the contents of the textbox (and then handle the blank input accordingly).

This also provides the added benefit of allowing the user to type in their date if they so choose.

TheTXI
  • 37,429
  • 10
  • 86
  • 110
1

Smart FieldPackEditor has a datepicker that is nullable. I believe it does everything that you need. I wish this was around when I was dealing with this sort of stuff. I still remember all the workarounds I had to implement with Microsoft's datepicker control. Uggh!

http://www.visualhint.com/index.php/fieldpackeditor/

Hector Sosa Jr
  • 4,230
  • 27
  • 30
-2

why not use a client side datepicker to populate a text field. If the textfield is empty, then you have a null date, otherwise convert the value.

jQuery has a nice easy to use datepicker. http://jqueryui.com

NerdFury
  • 18,876
  • 5
  • 38
  • 41
-3

This one seems to work, one of my co-workers had it:

using System;
using System.Windows.Forms;

namespace CustomControls
{
    public class NullableBindableDateTimePicker : System.Windows.Forms.DateTimePicker
    {
        private Boolean isNull = false;
        private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
        private Boolean ignoreBindOnFormat = false;

        public NullableBindableDateTimePicker()
        {
            this.Format = baseFormat;
            if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
        }

        public Boolean IsNull
        {
            get { return isNull; }
            set
            {
                isNull = value;
                this.Checked = value;
            }
        }

        //TODO: Add BaseCustomFormat

        public DateTimePickerFormat BaseFormat
        {
            get { return baseFormat; }
            set { baseFormat = value; }
        }

        public object BindMe
        {
            get
            {
                if (IsNull) return System.DBNull.Value;
                else return base.Value;
            }
            set
            {
                //String s = this.Name;

                if (ignoreBindOnFormat) return;

                if (System.Convert.IsDBNull(value))
                {
                    // for some reason setting base.format in this.format calls set BindMe.
                    // we need to ignore the following call
                    ignoreBindOnFormat = true;
                    this.Format = DateTimePickerFormat.Custom;
                    ignoreBindOnFormat = false;

                    this.CustomFormat = " ";
                    IsNull = true;
                }
                else
                {
                    ignoreBindOnFormat = true;
                    this.Format = baseFormat;
                    ignoreBindOnFormat = false;

                    if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
                    IsNull = false;
                    base.Value = (DateTime)value;
                }
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode == Keys.Delete)
            {
                this.BindMe = DBNull.Value;
            }
        }

        protected override void OnCloseUp(EventArgs eventargs)
        {
            base.OnCloseUp(eventargs);
            BindMe = base.Value;
        }
    }
}
Malfist
  • 31,179
  • 61
  • 182
  • 269