Click here to Skip to main content
13,299,495 members (60,454 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 17 Aug 2009

Nullable datetime column in .NET DataGrid with DateTimePicker

, 17 Aug 2009
Rate this:
Please Sign up or sign in to vote.
A nullable datetime column in .NET DataGrid with DateTimePicker.


I have always known a lot of people who have problems with the standard .NET 2.0 DataGridView and the nullable DateTime column (created with Oracle PL/SQL column definition):

dtReceived DATE

or a MS SQL Server DateTime column with NULL values in Transact SQL:


Google Search normally gives this Microsoft sample for calendar columns: How to: Host Controls in Windows Forms DataGridView Cells. This is a good start point, but it isn't enough. You can never correctly fill DateTime DataGridView cells with NULL. I found many variants for the simple DateTimePicker class, but work samples absent for nullable DateTime columns in a DataGridView.


I spent a lot of time on the Internet searching, and found a nice article about a nullable DateTimePicker: DateTimePicker DBNull. I then made several corrections and as 'magic touch', and it works for DataGridViews now.

Using the code

These are the steps we need:

  1. Rename the class DateTimePicker to DatePicker (the same name for different classes is a bad practice, in my opinion):
  2. public class DatePicker : System.Windows.Forms.DateTimePicker
  3. Add the following code snippet to the DatePicker class:
  4. public string ToShortDateString() 
        if (!realDate) 
            return String.Empty; 
            DateTime dt = (DateTime)Value; 
            return dt.ToShortDateString(); 
  5. Correct CalendarEditingControl.cs from the Microsoft Sample for usage in our DatePicker:
  6. class CalendarEditingControl : DatePicker, IDataGridViewEditingControl { ...
  7. Correct CalendarCell.cs from the Microsoft Sample for DBNull:
  8. public override void InitializeEditingControl(int rowIndex, object 
    initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, 
        CalendarEditingControl ctl = 
        DataGridView.EditingControl as CalendarEditingControl;
        // ++ vic 14-aug-2009
         object val = null;
              val = this.Value;
         catch (Exception ex)
              // Argument ot of range (value doesn't exists in collection)
         if (val != System.DBNull.Value)
              ctl.Value = (DateTime)val;

That's all, folks :)

You should press the DEL key to set a NULL filled DateTime cell.

The source code is attached. You can directly use the files from my project in your projects like in the Microsoft Samples (DatePicker.cs, CalendarCell.cs, CalendarColumn.cs, CalendarEditingControl.cs):

private void Form1_Load(object sender, EventArgs e)
     CalendarColumn col = new CalendarColumn();
     this.dataGridView1.RowCount = 5;
     foreach (DataGridViewRow row in this.dataGridView1.Rows)
          row.Cells[0].Value = DateTime.Now;

or from the Microsoft Visual Studio Data Designer for DataGrid (right mouse click on DataGridView, and choose 'Edit columns' from the context menu):

I have attached a real sample with database (Visual Studio 2008 / SQL Express database).


  • 14-Aug-2009: Initial version posted.
  • 16-Aug-2009: Database sample added.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Russian Federation Russian Federation
Microsoft certified professional developer (MCPD)

You may also be interested in...

Comments and Discussions

QuestionCreating New Row on Cell Click Pin
Member 1144181710-May-16 2:10
memberMember 1144181710-May-16 2:10 
QuestionCan't insert the calendar column value to SQL server Pin
Mozaharul Haque19-Mar-14 23:36
memberMozaharul Haque19-Mar-14 23:36 
AnswerCorrection Pin
Member 1003024528-Jan-14 19:50
memberMember 1003024528-Jan-14 19:50 
GeneralExcellent Article Pin
Vishnuvardhan Reddy M15-Dec-13 19:33
memberVishnuvardhan Reddy M15-Dec-13 19:33 
Excellent Article..saved a lot of time for me. Big Grin | :-D
QuestionCustom formating Pin
Member 85931649-Jul-13 5:12
memberMember 85931649-Jul-13 5:12 
GeneralThank you very much !! Pin
Mazen el Senih28-Jul-12 2:18
memberMazen el Senih28-Jul-12 2:18 
QuestionReallly good article! Pin
vishme27-Mar-12 1:12
membervishme27-Mar-12 1:12 
Questiondoubt! Pin
choosechrist26-Feb-12 22:14
memberchoosechrist26-Feb-12 22:14 
BugRow delete problem Pin
Member 817129618-Nov-11 7:38
memberMember 817129618-Nov-11 7:38 
QuestionSome bug... Pin
domivan24-Aug-11 5:37
memberdomivan24-Aug-11 5:37 
QuestionRe: Some bug... Pin
djax5-Mar-13 1:40
memberdjax5-Mar-13 1:40 
GeneralBrilliant! Thanks so much Pin
Daniel Neal12-May-11 1:46
memberDaniel Neal12-May-11 1:46 
Generalediting problem. Pin
Mohsin Hawra14-Jul-10 23:22
memberMohsin Hawra14-Jul-10 23:22 
GeneralRe: editing problem. Pin
Razze11-Oct-10 3:07
memberRazze11-Oct-10 3:07 
AnswerRe: editing problem. Pin
cje1235-Dec-10 17:41
membercje1235-Dec-10 17:41 
GeneralRe: editing problem. Pin
wouterdebo12-Jun-12 4:33
memberwouterdebo12-Jun-12 4:33 
GeneralGreat job Pin
khalil Youssef30-Jun-10 22:14
memberkhalil Youssef30-Jun-10 22:14 
GeneralGreat Job Pin
Mohammad Rafi Bin Abd Alim16-Jun-10 21:11
memberMohammad Rafi Bin Abd Alim16-Jun-10 21:11 
QuestionAllowing editing of the 'Time' portion of the DateTime [modified] Pin
KMalone11-May-10 23:00
memberKMalone11-May-10 23:00 
AnswerRe: Allowing editing of the 'Time' portion of the DateTime Pin
KMalone12-May-10 0:54
memberKMalone12-May-10 0:54 
GeneralRe: Allowing editing of the 'Time' portion of the DateTime Pin
Kim Bilida16-Jul-10 6:58
memberKim Bilida16-Jul-10 6:58 
GeneralGreat! Pin
Member 411002520-Mar-10 13:05
memberMember 411002520-Mar-10 13:05 
Generalthis.Value VS this.ToShortDateString() Pin
Drew Loika1-Feb-10 12:38
memberDrew Loika1-Feb-10 12:38 
GeneralRe: this.Value VS this.ToShortDateString() Pin
vic_ch20001-Feb-10 19:23
membervic_ch20001-Feb-10 19:23 
Generalthank you Pin
Samir the Boy13-Jan-10 1:06
memberSamir the Boy13-Jan-10 1:06 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 17 Aug 2009
Article Copyright 2009 by vic_ch2000
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid