DateTime Picker null value


  • Hi All

    Is it possible to display null values (empty) in datatimepicker control.

    If the value does not exist in the database , it should display blank .


    ./Thanks in advance




    Wednesday, October 24, 2007 5:31 AM


All replies

  • Hi Preshweenet,


    This is not possible.

    DateTimePicker.Value must be between DateTime.MinValue and DateTime.MaxValue.Otherwise exception will be raised.




    Wednesday, October 24, 2007 6:11 AM
  • Hi,


    Is it not possible to display null values in datetimepicker.

    Datetimepicker always show some valid datetime.



    Manju Sandhu

    Wednesday, October 24, 2007 6:19 AM

    Hi again

    Is there any other control which serve the same purpose of DataTimepicker with the null value support


    /Thansk in adance


    Wednesday, October 24, 2007 8:04 AM



    One workaround is that you use three domainUpDown. One for day,another for month and last for year.

    In all of them you can have one item blank.


    If i find any better solution I will inform you that.



    Manju Sandhu

    Wednesday, October 24, 2007 9:16 AM
  • Hi, Prashweenet,


    Based on my understanding, you want to find a DateTimerPicker which can support null value, don't you?


    Well, the standard DateTimePicker does not support this feature,

    however there are a lot of custom control which can support it.


    And Manju Sandhu is right, you can use a MonthCalendar, a DropDownBox to build your own DateTimePicker which can support null value.



    Hope this helps,



    Friday, October 26, 2007 8:45 AM
  • You can use the checbox property of the datetimepicker


    When you save to your database you do


    Dpar.IsNullable = True

    Dpar.Value = IIf(DTP1.Checked, DTP1.Value, DBNull.Value)



    When you read from your database you do

    Me.DTP1.Value = IIF(tabla.Rows(0)(row(0)("field").ToString)

    Is DBNull.Value, Date.Now, tabla.Rows(0)(row(0)("field").ToString))

    Me.DTP1.Checked = Not (tabla.Rows(0)(row(0)("field").ToString) Is DBNull.Value)

    Monday, April 14, 2008 2:20 PM
  • I have had the similar problem. I did the workaround is that, when the user inputs nothing in the datetime field va lues, I make the datetime variable to maxvalue and save it to database, and when retrieiving from the database, I checked if its max value then display nothing.
    Monday, April 14, 2008 2:29 PM
  • For set a empty value in a DateTimePicker control use the CustomFormat:

    this.dateTimePicker.CustomFormat = " "// use a space 
    this.dateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom; 

    Best regards, Sergiu
    Tuesday, January 20, 2009 11:35 AM
  • I have used Sergiu solution, the only problem is when I need to pus some value in the datetimeicker using the calendar i dont get any change of text. I dont know on what event to do the change of format
    Friday, January 28, 2011 9:53 AM

  • this.dateTimePicker.CustomFormat = " "// use a space 

    This will throw an error: "Object reference not set to an instance of an object." - of course, date format cannot be set to some null or emply space value. Maybe this way possible inth .net 3.0 and older, but not int 3.5 and 4.0.


    BARBI: what exactly would you like to achive?

    • Proposed as answer by Quickdraw Friday, December 23, 2011 5:59 AM
    • Unproposed as answer by Quickdraw Friday, December 23, 2011 5:59 AM
    Friday, January 28, 2011 10:31 AM
  • Wow, did I burn a ton of time on this one!

    I found that you can databind to the Tag property, and it will take DBNull.Value, which makes the database do the right things. Then it was on to the task of keeping the picker Value and Tag properties in sync, and dealing with all manner of UI/event weirdness. Thanks Microsoft, for not implementing null for this control, and double thanks for not fixing it after all these years.

    One of the problems is that by using CustomFormat, we hard code the display format of DateTime values in the picker, losing features of locale. But switching the Format property on the fly has serious issues: it causes databinding and the enter event to happen again! More problems: using ValueChanged event is no good. It fires when you change the format (because technically, the Text property does change when you change the format). Also, when Leave fires, Enter also fires (without firing Leave again).

    To deal with these sad facts there is much to do. Here is my vanilla implementation of the solution. Not for those looking for an easy fix:



    // Make sure picker databinds to Tag prop, not Value prop!
    // Make sure to set picker.CustomFormat to a single space (" " w/o quotes)
    namespace SEITracker
      public partial class MyForm : SEITracker.BaseForm
        // Since Enter event fires when leaving and when changing format, 
        // use a pseudo-semaphore to signal the Enter event not to muck things up
        bool DateTimePickerBinding = false; 
        DateTime nullDate = new DateTime(1900, 1, 1);
        DateTimePickerFormat dateTimePickerFormat = DateTimePickerFormat.Short;
        string dateTimePickerNullFormat = " ";  	
        private void Idea_Load(object sender, EventArgs e)
          // You can set these in the designer and skip these 2 lines
          picker1.Format = dateTimePickerFormat;
          picker1.CustomFormat = " ";  // Signifies null
          ...<br/>    }
        // This is how the user signals to empty the field. You
        // can use the checkbox and it's Checked event instead
        // i.e., if (!picker.Checked) SetDateTimePickerNull(picker);
        // The important thing here is to set the pseudo-semaphore
        // before and unset after. See comments below
        private void picker1Label_Click(object sender, EventArgs e)
          // Since Enter event fires when *leaving* AND when *changing format*, 
          // use a pseudo-semaphore to signal the Enter event not to muck things up
          DateTimePickerBinding = true;
          DateTimePickerBinding = false;
        private void DateDateTimePicker_Enter(object sender, EventArgs e)
          DateTimePicker picker = (DateTimePicker)sender;
          // If value empty (user sees blank value), set default value and format for picking a value
          if (picker.Format == DateTimePickerFormat.Custom && !DateTimePickerBinding)
            // Because changing the format will cause binding to the datasource,
            // all we have to do is set the bound property (Tag) and change the format
            picker.Value = DateTime.Parse(UTCDateTime.Now());
            // Warning: Causes rebinding, and kills message to picker to expand.
            // user would get the current date filled in on first click, then user
            // will have to click again to get the calendar to pop out
            picker.Format = dateTimePickerFormat;
            // Solution: (Thanks Zep @
            SendKeys.Send("%{DOWN}"); // Opens calendar again
        private void DateDateTimePicker_Leave(object sender, EventArgs e)
            DateTimePicker picker = (DateTimePicker)sender;
            if (picker.Value == nullDate)
              // Hide display of value from the user
              if (picker.Format != DateTimePickerFormat.Custom) 
                picker.Format = DateTimePickerFormat.Custom;
              // Sync with the databound property
              picker.Tag = picker.Text;
        private void SetDateTimePickerNull(DateTimePicker picker)
          picker.Tag = DBNull.Value;
          picker.Value = nullDate;
          // POOF! It's Magic! Don't worry, this sets currentvalue only, so ADO
          // does pick up the change when Update() called on the datasource
          // Warning: Causes rebinding. So, we have to sync the datasource manually
          // with the line above before changing the format, else the Value prop gets
          // reset to the db value when EndEdit() called 
          // Also, this causes the Enter event to fire. Crap! That causes the date
          // to flip back to a value with the SendKeys in the Enter event
          // Solution: set DateTimePickerBinding semaphore before calling this function
          picker.Format = DateTimePickerFormat.Custom;


    • Edited by Quickdraw Friday, December 23, 2011 10:22 AM code change
    Friday, December 23, 2011 10:20 AM