I encountered this problem when I was working on the project today. After searching for examples on the internet, I finally found a suitable one from csdn for a long time. Now I have published it, for your reference (just select the-_-of the date -_-)
1. First declare the DateTimePicker object (public)
Private DateTimePicker dTimePicker = new DateTimePicker ();
2. Add the following code in Form_Load:
DTimePicker. Visible = false; // not displayed by default
DTimePicker. Format = DateTimePickerFormat. Custom; // The DateTimePicker Format is Custom dTimePicker. CustomFormat = "yyyy-MM-dd HH: mm: ss"; // DateTimePicker Custom type
DTimePicker. TextChanged + = new EventHandler (dTimePicker_TextChanged); // value assignment when DateTimePicker text changes
Private void dTimePicker_TextChanged (object sender, EventArgs e)
{
// Assign the Text value of dTimePicker to the selected cell value of dataGridView1
DgvFKDB. CurrentCell. Value = dTimePicker. Text;
}
Private void dgvFKDB_CurrentCellChanged (object sender, EventArgs e)
{
DataGridViewCell CurrnetCell = this. dgvFKDB. CurrentCell;
If (CurrnetCell! = Null & CurrnetCell. OwningColumn. Name = "FKSJ ")
{
// Obtain the rectangle of the display area of the selected cell in dataGridView1
Rectangle Rect = this. dgvFKDB. GetCellDisplayRectangle (CurrnetCell. ColumnIndex, CurrnetCell. RowIndex, true );
// DTimePicker is displayed in the rectangle of the display area of the selected cell in dataGridView1, that is, within the selected cell
DTimePicker. Visible = true;
DTimePicker. Top = Rect. Top;
DTimePicker. Left = Rect. Left;
DTimePicker. Height = Rect. Height;
DTimePicker. Width = Rect. Width;
// The dTimePicker control obtains the value of the selected cell in the initial value of dataGridView1.
DTimePicker. Text = dgvFKDB. Rows [CurrnetCell. RowIndex]. Cells [CurrnetCell. ColumnIndex]. Value. ToString ();
}
Else
{
This. dTimePicker. Visible = false;
}
}
This example is adapted from csdn.
Download source code http://www.bkjia.com/uploadfile/2012/0321/20120321083518126.rar (csdn above, can refer)
From yilou xianyu