Is there any way to put a DateTimePicker control in the DataGridView?
I checked all the possible properties but it give option of checkbox, combo box etc, but not the DateTimePicker.
You haven't missed any built-in option, but it is possible to subclass both the DataGridViewColumn
and DataGridViewCell
classes to host any control of your choosing.
This article on MSDN explains the process in more detail, and even includes some sample code:
How to: Host Controls in Windows Forms DataGridView Cells
You can also find a complete sample on Code Project: Generic DataGridView V2.0
private void dgtest_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dtp = new DateTimePicker();
dgtest.Controls.Add(dtp);
dtp.Format = DateTimePickerFormat.Short;
Rectangle Rectangle = dgtest.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(Rectangle.Width, Rectangle.Height);
dtp.Location = new Point(Rectangle.X, Rectangle.Y);
dtp.CloseUp += new EventHandler(dtp_CloseUp);
dtp.TextChanged += new EventHandler(dtp_OnTextChange);
dtp.Visible = true;
}
}
private void dtp_OnTextChange(object sender, EventArgs e)
{
dgtest.CurrentCell.Value = dtp.Text.ToString();
}
void dtp_CloseUp(object sender, EventArgs e)
{
dtp.Visible = false;
}
To solve some entry issues when using the DateTimePicker in a DataGridView you will want to add the following to the Microsoft Sample referenced above. It took quite a while to search out the problems with the valuechanged event not firing as expected. The fix came from here (stackoverflow) and translated to C# below. It seemed appropriate to add this information here as I keep finding this forum post when searching on DataGridView and DateTimePicker.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Enter:
case Keys.Tab:
this.dataGridView.Focus();
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Maybe this isn't proper, but easy trick and same result........ lot less code......I was just playing around and though outside the box, just set
I hide mine until they click cell, or you can show First I declared :
DateTimePicker1.Visible = False
when you click in cell, run this code...
DateTimePicker1.Visible = True
ActiveControl = DateTimePicker1
Then below
Public Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
requestDGV.Rows(0).Cells("requestTimeOff").Value = (DateTimePicker1.Value)
DateTimePicker1.Visible = False
DateTimePicker1.Enabled = False
End Sub
Super Basic , and i have it sitting directly in the box, doesn't look out of place
Or super easy mode.......I just like to hide mine till column click
Public Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
requestDGV.Rows(0).Cells("requestTimeOff").Value = (DateTimePicker1.Value)
End Sub
You really just need that one line.....data will be in the grid, just a lot less code.....
I think the CloseUp event on DateTimePicker is more appropriate because the changed value is triggering on any change while CloseUp only triggers when the entire date is selected
来源:https://stackoverflow.com/questions/4815677/how-can-i-display-a-datetimepicker-in-a-datagridview