How to Add DateTimePicker Column Using C#
Submitted by janobe on Friday, May 31, 2019 - 21:52.
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.
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.
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 isdtg_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)
- {
- dtg_Date.Controls.Add(datePicker);
- datePicker.Format = DateTimePickerFormat.Short;
- Rectangle Rectangle = dtg_Date.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
- datePicker.Visible = true;
- }
- }
Step 6
Write the following codes for thetextChange
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 thecloseUp
event of the DateTimePicker.
- void datePicker_CloseUp(object sender, EventArgs e)
- {
- datePicker.Visible = false;
- }
Add new comment
- 355 views