有没有办法将DateTimePicker控件放在DataGridView中?
我检查了所有可能的属性,但它提供了复选框,组合框等选项,但不提供DateTimePicker。
答案 0 :(得分:26)
您没有错过任何内置选项,但可以将DataGridViewColumn
和DataGridViewCell
类子类化为托管您选择的任何控件。
MSDN上的这篇文章更详细地解释了这个过程,甚至还包括一些示例代码:
How to: Host Controls in Windows Forms DataGridView Cells
您还可以在代码项目中找到完整的示例:Generic DataGridView V2.0
答案 1 :(得分:5)
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;
}
答案 2 :(得分:1)
要解决在DataGridView中使用DateTimePicker时的一些输入问题,您需要将以下内容添加到上面引用的Microsoft示例中。花了很长时间才找出valuechanged事件未按预期触发的问题。修复来自这里(stackoverflow)并转换为下面的C#。在这里添加这些信息似乎很合适,因为我在搜索DataGridView和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);
}
答案 3 :(得分:1)
也许这不是正确的,但是简单的技巧和相同的结果........少了很少的代码......我只是在玩,虽然在盒子外面,只是设置
我隐藏它直到他们点击单元格,或者你可以显示 首先我宣布:
DateTimePicker1.Visible = False
单击单元格时,运行此代码...
DateTimePicker1.Visible = True
ActiveControl = DateTimePicker1
然后在
下面 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,我把它直接放在盒子里,看起来不合适
或超级简单的模式.......我只是想隐藏我的直到列点击
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
你真的只需要一行.....数据将在网格中,只需要少得多的代码......
答案 4 :(得分:1)
好吧...使用@rajat和@Aaron的一些示例,我制作了一个在DateTimePicker单元中弹出的示例。谢谢大家。
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 8 Then 'CHECK IF IT IS THE RIGHT COLUMN
'SET SIZE AND LOCATION
Dim rect = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
DateTimePicker1.Size = New Size(rect.Width, rect.Height)
DateTimePicker1.Location = New Point(rect.X + 10, rect.Y + 76) 'USE YOU OFFSET HERE
DateTimePicker1.Visible = True
ActiveControl = DateTimePicker1
End If
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
If DataGridView1.RowCount > 0 Then 'JUST TO AVOID FORM LOAD CRASH
DataGridView1.CurrentCell.Value = DateTimePicker1.Value.ToShortDateString
DateTimePicker1.Visible = False
End If
答案 5 :(得分:0)
我认为DateTimePicker上的CloseUp事件更合适,因为更改的值会在任何更改时触发,而CloseUp仅在选择整个日期时触发