C # -- DataGridView Add the DateTimePicker time control,
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 (); // instantiate a DateTimePicker control Rectangle _ Rectangle; public Form1 () {InitializeComponent (); dataGridView1.Controls. add (dtp); // Add the time control to the DataGridView dtp. visible = false; // do not display dtp first. format = DateTimePickerFormat. custom; // set the date format, dtp. textChanged + = new EventHandler (dtp_TextChange ); // Add the event dtp_TextChange}/**************** when the time control is selected ************ * ***/private void dtp_TextChange (object sender, eventArgs e) {maid. value = dtp. text. toString (); // when the time control is selected, grant the time content to the cell}/****************** the cell is clicked, determines whether the column of the time control is *******************/private void datagridviewinclucellclick (object sender, DataGridViewCellEventArgs 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; // display control} else dtp. visible = false;}/*********** when the column width changes, the time control is hidden first, otherwise, the time control cannot be changed when the cell width and height change. */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 is also hidden, otherwise, the time control location will be messy. *******/private void dataGridView1_Scroll (object sender, ScrollEventArgs e) {dtp. visible = false ;}}}