• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Add DateTimePicker Column Using C#

Serpico

Asynchronous Code Expert
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
51
Likes
140
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
Here’s another tutorial that beginners in programming will find it very useful when they are writing codes. In this tutorial, I’m going to teach you how to add DateTimePicker Column using C#. This is just a simple method that can view the datetimepicker in a column and select the date that you need. Just follow the guide below and you will see how it works.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2

Do the form just like shown below.
dtp_ps2.png

Step 3

Press F7 to open the code editor. In the code editor, instantiate the object that is needed.

  1. DateTimePicker datePicker =

    new

    DateTimePicker(

    )

    ;

Step 4

Click the DataGridView and go to the properties. In the properties, hit the event just like a lightning bolt. Scroll down and double click the event which is dtg_Date_CellClick

to open the code editor.
dtgevent.png

Step 5

In the code editor, write the following codes to create a DateTimePicker column when the cell is clicked.

  1. private

    void

    dtg_Date_CellClick(

    object

    sender, DataGridViewCellEventArgs e)
  2. {
  3. if

    (

    e.

    ColumnIndex

    ==

    0

    )
  4. {
  5. datePicker =

    new

    DateTimePicker(

    )

    ;
  6. dtg_Date.

    Controls

    .

    Add

    (

    datePicker)

    ;
  7. datePicker.

    Format

    =

    DateTimePickerFormat.

    Short

    ;

  8. Rectangle Rectangle =

    dtg_Date.

    GetCellDisplayRectangle

    (

    e.

    ColumnIndex

    , e.

    RowIndex

    , true

    )

    ;
  9. datePicker.

    Size

    =

    new

    Size(

    Rectangle.

    Width

    , Rectangle.

    Height

    )

    ;
  10. datePicker.

    Location

    =

    new

    Point(

    Rectangle.

    X

    , Rectangle.

    Y

    )

    ;

  11. datePicker.

    CloseUp

    +=

    new

    EventHandler(

    datePicker_CloseUp)

    ;
  12. datePicker.

    TextChanged

    +=

    new

    EventHandler(

    datePicker_OnTextChange)

    ;


  13. datePicker.

    Visible

    =

    true

    ;
  14. }
  15. }

Step 6

Write the following codes for the textChange

event of the DateTimePicker.

  1. private

    void

    datePicker_OnTextChange(

    object

    sender, EventArgs e)
  2. {

  3. dtg_Date.

    CurrentCell

    .

    Value

    =

    datePicker.

    Text

    .

    ToString

    (

    )

    ;
  4. }

Step 7

Write the following codes for the closeUp

event of the DateTimePicker.

  1. void

    datePicker_CloseUp(

    object

    sender, EventArgs e)
  2. {
  3. datePicker.

    Visible

    =

    false

    ;
  4. }

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

350,639

350,649

Top