Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

C++ DateTimePicker in DataGridView

5.00/5 (4 votes)
15 Oct 2010CPOL2 min read 47.9K   3.5K  
C++ DateTimePicker in DataGridView
Image 1

Introduction

I searched high and low for an article in Windows Forms using C++ .NET to demonstrate how to add a datetimepicker to a DataGridView, and for added spice, allow null values. There is an abundance of articles using C#, but no C++.

WHAT

This article aims to replicate the widely available Microsoft article in C++ using Visual Studio 2008 and .NET 3.5. Using it as an example will enable a C++.NET developer to deploy a nullable DateTimePicker in the DataGridView.

HOW

So what we have as our starting point is the much referenced Microsoft article in C#: http://msdn.microsoft.com/en-us/library/7tas5c80.aspx.

The next ingredient into the mix is Tangible Software Solutions http://www.tangiblesoftwaresolutions.com/ demo C# to C++ converter.

But the demo can only process 100 lines and the Microsoft sample is 246 lines. So, first to go were the white space lines, following which all comments were deleted, and finally many line feeds were removed so that I had multiple statements on the same line.

Here's an example:

C++
public CalendarCell()
        : base()
    {
        // Use the short date format.
        this.Style.Format = "d";
    }

became:

C++
public CalendarCell(): base()

    {        this.Style.Format = "d";    }

After that, it was a matter of starting a new Windows Form application in Visual Studio 2008, and pasting in the translated code to the .h and .cpp files. There was only 1 bug to be corrected in the translated code. Main had to be changed to main. This is DataGridDateTimeEx with code exactly as translated from the Microsoft site. That’s all it took to generate a working DateTimePicker in a DataGridView written with C++ .NET.

DataGridViewDateTimeEx2 in the initial post was a slightly personalized version, e.g., the form and the DataGridView are dropped on from the toolbox, the from_load event is in the .h file on this occasion, the date/time column was moved to the third column and given a column header.

I have replaced it with DataGridViewDateTimeEx5. It still uses the same translated code, but now each of the classes, CalendarCell, CalendarColumn and CalendarEditingControl have their own .h and .cpp files. This both aids reusability and keeps the application specific .h and .cpp ‘clean’.

From here, the translated classes can be further farmed out into their own class library for easy reuse across multiple modules. Calendar Column (provided it is declared as public) is now added to the Column Type dropdown allowing you to specify where you want your date column using the Form Designer.

Image 2

As a further enhancement, I have added a button column which clears one of the date columns. I had a look at the CodeProject article by vic_ch2000 on the subject, but I was unable to reliably replicate his DatePicker class in C++ so went for the Button approach. I added CellContendClickEvent to the grid and programmed it as follows:

C++
private: System::Void dataGridView1_CellContentClick
(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
 if (e->ColumnIndex != 4)
     return; // Hardcoded to only accept the event on the button column
 else
      dataGridView1->Rows[e->RowIndex]->Cells[e->ColumnIndex-1]->Value = nullptr;
 }

History

  • 13th October, 2010: Initial version

License

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