Click here to Skip to main content
Click here to Skip to main content

C++ DateTimePicker in DataGridView

, 15 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
C++ DateTimePicker in DataGridView

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:

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

became:

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.

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:

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)

Share

About the Author

Ger Hayden
Software Developer
Ireland Ireland
My first program was written in Basic on a Sinclair Spectrum ZX 16K in the summer of '85. Having studied Computer Systems I attempted to break into the world of C but took a wrong turn and got immersed in COBOL!
 
I looked a C again in 1994 but didnt follow up on it. In 2001 I introduced myself to Visual C++ 6.0 courtesy of Ivor Hortons book, but found the going difficult. I tipped my toe in the .NET water in '05 but the first example I tried in VC++ 2005 express didnt work and allied with the absence of MFC in the express package, I parked that up.
 
Along the way my career got shunted into software testing
 
A personal machine change force me to migrate to VS2008 in 2008. The new edition of Ivor Hortons book for VC++ in VS2008 reintroduced me to .NET and I got curious whereupon I went out and acquired Stephen Fraser's "Pro Visual C++/CLI and
the .NET 3.5 Platform". I am hooked!
 
After 20 years I think I have found my destination.

Comments and Discussions

 
QuestionExcellent article - The best way for changing to display time, date in one datagridview? Pinmembernikonation5-Nov-12 4:20 
GeneralMy vote of 5 PinmemberHardryv22-May-12 9:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 15 Oct 2010
Article Copyright 2010 by Ger Hayden
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid