Http://blog.csdn.net/diyoosjtu/article/details/7584857
Because the columntype provided by the datagridview does not contain the datetimepicker control. Therefore, it is more difficult to implement columns with input dates. You can add the datetimepicker control to the datagridview by using the following methods.
First, add a datagridview control to the front-end design and name it datagridview1. (Text/piikee)
Then, write the following code to the background. CS file:
[CSHARP]
View plaincopyprint?
- Using system;
- Using system. Collections. Generic;
- Using system. componentmodel;
- Using system. Data;
- Using system. drawing;
- Using system. LINQ;
- Using system. text;
- Using system. Windows. forms;
- Namespace moonlight_treasure
- {
- Public partial class mycount: Form
- {
- Datetimepicker DTP = new datetimepicker (); // A datetimepicker control is instantiated here.
- Rectangle _ rectangle;
- Public mycount ()
- {
- Initializecomponent ();
- Datagridview1.controls. Add (DTP); // Add the time control to the datagridview
- DTP. Visible = false; // do not show it first
- DTP. format = datetimepickerformat. Custom; // you can specify the date format as 2010-08-05.
- DTP. textchanged + = new eventhandler (dtp_textchange); // Add the event dtp_textchange for the Time Control
- }
- ****************/
- Private void dtp_textchange (Object sender, eventargs E)
- {
- Datagridview1.currentcell. value = DTP. Text. tostring (); // when the time control is selected, the time is assigned to the corresponding cell.
- }
- /**************** The cell is clicked, determines whether the column of the time control is *******************/
- Private void maid cellclick (Object sender, maid E)
- {
- If (E. columnindex = 0)
- {
- _ Rectangle = maid (E. columnindex, E. rowindex, true); // obtain the location and size of the cell
- DTP. size = new size (_ rectangle. Width, _ rectangle. Height); // assign the cell size to the time control.
- DTP. Location = new point (_ rectangle. X, _ rectangle. Y); // assign the cell position to the time control.
- DTP. Visible = true; // controls can be displayed.
- }
- Else
- DTP. Visible = false;
- }
- /*********** When the column width changes, the time control is hidden first, otherwise, the control cannot grow as the cell grows ***********/
- Private void datagridview1_columnwidthchanged (Object sender, datagridviewcolumneventargs E)
- {
- DTP. Visible = false;
- }
- /*********** When the scroll bar is rolling, the cell position changes and the time control must be hidden, otherwise, the time control location will be messy ********/
- Private void datagridview1_scroll (Object sender, scrolleventargs E)
- {
- DTP. Visible = false;
- }
- }
- }