Serpico
Asynchronous Code Expert
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#.
Step 2
Do the form just like shown below.
Step 3
Press F7 to open the code editor. In the code editor, instantiate the object that is needed.
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.
Step 5
In the code editor, write the following codes to create a DateTimePicker column when the cell is clicked.
Step 6
Write the following codes for the textChange
event of the DateTimePicker.
Step 7
Write the following codes for the closeUp
event of the DateTimePicker.
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
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.

Step 2
Do the form just like shown below.

Step 3
Press F7 to open the code editor. In the code editor, instantiate the object that is needed.
- 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.

Step 5
In the code editor, write the following codes to create a DateTimePicker column when the cell is clicked.
- private
void
dtg_Date_CellClick(
object
sender, DataGridViewCellEventArgs e)
- {
- if
(
e.
ColumnIndex
==
0
)
- {
- datePicker =
new
DateTimePicker(
)
;
- dtg_Date.
Controls
.
Add
(
datePicker)
;
- datePicker.
Format
=
DateTimePickerFormat.
Short
;
- Rectangle Rectangle =
dtg_Date.
GetCellDisplayRectangle
(
e.
ColumnIndex
, e.
RowIndex
, true
)
;
- datePicker.
Size
=
new
Size(
Rectangle.
Width
, Rectangle.
Height
)
;
- datePicker.
Location
=
new
Point(
Rectangle.
X
, Rectangle.
Y
)
;
- datePicker.
CloseUp
+=
new
EventHandler(
datePicker_CloseUp)
;
- datePicker.
TextChanged
+=
new
EventHandler(
datePicker_OnTextChange)
;
- datePicker.
Visible
=
true
;
- }
- }
Step 6
Write the following codes for the textChange
event of the DateTimePicker.
- private
void
datePicker_OnTextChange(
object
sender, EventArgs e)
- {
- dtg_Date.
CurrentCell
.
Value
=
datePicker.
Text
.
ToString
(
)
;
- }
Step 7
Write the following codes for the closeUp
event of the DateTimePicker.
- void
datePicker_CloseUp(
object
sender, EventArgs e)
- {
- datePicker.
Visible
=
false
;
- }
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.