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 test{ Public partial class form1:form { datetimepicker DTP = new DateTimePicker () ; //instantiation of a DateTimePicker control rectangle _rectangle; Public Form1 () { InitializeComponent (); & nbsp DATAGRIDVIEW1.CONTROLS.ADD (DTP); //Add Time controls to datagridview DTP . Visible = false; //Do not display DTP. format = datetimepickerformat.custom; //Set Date format,2017-11-11 DTP. TextChanged + = new EventHandler (dtp_textchange); Adding events to a Time control dtp_textchange } /************* time control when selecting time ****************/private void Dtp_textchange (object sender, EventArgs e) { DataGridView1.CurrentCell.Value = DTP. Text.tostring (); When the time control selects the time, assigns the time content to the cell in which it is located}/**************** cell is clicked to determine if the column of the time control is placed *******************/ private void datagridview1_ CellClick (object sender, DataGridViewCellEventArgs e) { &NBSP ; if (E.columnindex = = 0) { _rectangle = Datagridview1.getcelldisplayrectangle (E.columnindex, E.rowindex, true); Get the location and size of your 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 cell locations to time controls DTP. Visible = true; //Display controls } ELSE&NBSP ; DTP. Visible = false; }/*********** when the width of a column changes, the time control is first hidden, otherwise the time control cannot be changed ***********/private void datagridview1_columnwidthchanged When the height of the cell changes. Object sender, DataGridViewColumnEventArgs e) {DTP. Visible = false; }/*********** the scrollbar is scrolled, the cell position changes and the time control is hidden, otherwise the time control position does not move ********/private void Datagridview1_scroll (object sender, Scroll EventArgs e) {DTP. Visible = false; } }}
C#--datagridview Adding DateTimePicker Time Controls