Click here to Skip to main content
14,365,424 members

# Nullable DateEdit Control

Rate this:
4.13 (8 votes)
Please Sign up or sign in to vote.
4.13 (8 votes)
15 Jun 2003
A Nullable C# DateEdit Control

Sample Image - DateEdit.gif


The .NET DateTimePicker (DTP) control is quite good, until you need a nullable date. I recently came to a need for a nullable date and discovered this problem. A good example would be ShippedDate. What should this property contain if the product has not shipped? On a related topic, the DateTime structure doesn't support a null value. .NET Beta's had a DateTime.Empty value but this was removed. I've seen some workarounds with the DTP by using the checkbox to indicate a null value but I would expect some confusion from end users. The DateEdit control is the result of my efforts find a solution to this problem. It shows an empty format "__/__/____" when the value is null. The focus of this control is quick and direct data entry. It currently doesn't provide a drop down calendar, but I have an idea to build an different control for those who need more "eye candy".

Design Considerations

Invalid Dates

Stopping invalid date values from being entered can be difficult. For an example, try entering 2/29/2000 into the DTP. For this reason, I decided not to attempt any validation of the user typing other than allowing only numeric digits. This means that 03/45/____ could be entered into the DateEdit control. To handle this problem, I fire an InvalidDate event from the OnLeave event. This can be used to display a message, or change the back color, or whatever method you prefer to notify the user of invalid values.

Non Nullable DateTime Struct

The DateTime structure does not allow Null values. After spending some time considering writing a NullableDateTime structure, I decided against it. In order for this to work well, it would need to be an exact replacement for the standard DateTime struct. Since a struct cannot be inherited, you will need to implement EVERY method contained in the DateTime struct. One relatively straightforward solution to this problem is to define a new DateTime constant NullValue that equals MinValue ( 01/01/0001 ). This prevents use of MinValue as a valid date but I think I can live with this.

Null Date Persistance

The next issue is how to handle NullValue dates when they are persisted to your database. You could simply persist the 01/01/0001 to the database and remember that this value represents Null. I'm not fond of this solution since someone writing queries against the database might not know this. What would the SQL look like if I wanted to know which orders have not shipped?

Select ... Where ShippedDate Is Null
Select ... Where ShippedDate = 1/1/0001
I think the first SQL looks more logical. Also, if someone is looking at raw columnar data from the table they would see 1/1/0001, which looks like a real date. Null values would be more obvious.

Persisting NullValue (1/1/0001) to the database does have the advantage that no translation to DBNull is necessary.

Persisting Null dates means some additional work. A DateTime struct that supported Null values and would convert to DBNull automatically would be the ideal solution but since DateTime cannot be null, we have to use a workaround. (Read more work for you) The workaround is to add code during database retrieval and persistence to convert NullValue to DBNull.

DateEdit Features

I have incorporated handling for international date formatting and performed "some" testing on this feature. Let me know if you find any bugs.

The DateEdit control is based on the MaskedEdit control. It uses 2 digits for month and day. This means February 5 must be entered 02/05. This adds a couple of sometimes unnecessary keystrokes during date entry.

The IsValid property returns true for valid dates or Null.

Use the Value property to get/set the DateTime value. Note: 1/1/0001 or whatever you decide to use as the NullValue is automatically converted to the blank mask. "__/__/____" depending on your regional settings.

Next Steps

The DateEdit control is designed for simple date entry. Addition of a drop down month calendar would make it more visually appealing. The month calendar can be handy if you're entering dates close to a known date such as +- 1 month from the current date. Entering a birth date in the DTP with any default date by using the month calendar could mean traversing 10, 20, 30 years, 1 month at a time. Not likely to be very fast. Adding a drop down month calendar would mean building a composite control based on the DateEdit, the built-in MonthCalendar and a down arrow button. Let me know what you think. The DateEditEx control would be separate so that if you don't need the added size and overhead of the drop down MonthCalendar then you don't have to use it.

The DateEdit control doesn't currently support time entry. My experience tells me that dates that require time values are best set internally from code. Time entry would add additional typing from the user and I wouldn't expect a high degree of accuracy.

This isn't a perfect solution, workable hopefully. Let me know your ideas for improvement.


  • Initial Release: June 16th, 2003


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Oscar Bowyer
Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralUse SqlDateTime object Pin
dokmanov12-Oct-07 22:13
memberdokmanov12-Oct-07 22:13 
GeneralRe: Use SqlDateTime object Pin
Oscar Bowyer17-Oct-07 5:26
memberOscar Bowyer17-Oct-07 5:26 
GeneralDataset changes without any user operation Pin
Claudio Barca7-Jan-04 7:53
memberClaudio Barca7-Jan-04 7:53 
GeneralNullableTypes.NullableDateTime Pin
Anonymous8-Sep-03 9:58
memberAnonymous8-Sep-03 9:58 
GeneralRe: NullableTypes.NullableDateTime Pin
John N9-May-04 21:28
memberJohn N9-May-04 21:28 
GeneralI do not agree with using DateTime.MinValue Pin
Eric Swanson1-Jul-03 8:53
memberEric Swanson1-Jul-03 8:53 
GeneralDateEdit Update Pin
Oscar Bowyer21-Jun-03 12:18
memberOscar Bowyer21-Jun-03 12:18 
GeneralRe: DateEdit Update Pin
ru15-Jul-03 0:07
memberru15-Jul-03 0:07 
GeneralOther alternative Pin
Sebastien Lorion19-Jun-03 6:08
memberSebastien Lorion19-Jun-03 6:08 
QuestionWhat I did... Pin
Martin Gibson17-Jun-03 13:41
memberMartin Gibson17-Jun-03 13:41 
GeneralI agree with the Min date value.. Pin
Rocky Moore17-Jun-03 0:58
memberRocky Moore17-Jun-03 0:58 
GeneralFood for thought Pin
mav.northwind16-Jun-03 21:28
membermav.northwind16-Jun-03 21:28 
GeneralRe: Food for thought Pin
Martin Gibson17-Jun-03 13:44
memberMartin Gibson17-Jun-03 13:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 15 Jun 2003


34 bookmarked