views:

472

answers:

4

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?

A: 

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
sorry, this is for a winforms app, not web.
Malfist
It's silly to assume he was talking about a webapp when he's talking about C#.
belgariontheking
Oops, yeah sorry about that. The most up-voted answer now is using the same concept though, so obviously the concept is valid. My head is just stuck in internet land since that's where I spend my days.
NerdFury
+4  A: 

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
Agreed. Or have a checkbox that enables and disables it. Don't over-complicate things.
Pesto
The best solution is often times the simplest solution.
TheTXI
+1  A: 

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/

hectorsosajr
A: 

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
I assume you asked him before posting his code here...
Shog9
aye, I did so. He did not mind.
Malfist