简体   繁体   English

如何在C#winforms的DataGridView控件中托管datetimepicker

[英]How to host datetimepicker in DataGridView control in C# winforms

private void showdate(DataGridViewCellEventArgs e)
{
    dateTimePicker1.Size = vacation_transDataGridView.CurrentCell.Size;
    dateTimePicker1.Top = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + vacation_transDataGridView.Top;
    dateTimePicker1.Left = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + vacation_transDataGridView.Left;

    if (!(object.Equals(Convert.ToString(vacation_transDataGridView.CurrentCell.Value),"")))
    {
        dateTimePicker1.Value = Convert.ToDateTime(vacation_transDataGridView.CurrentCell.Value);
        //dateTimePicker1.Visible = false;
    }

    dateTimePicker1.Visible = true;
}

This code in dgv cell_click event dgv cell_click事件中的这段代码

There is an example of exactly this thing on MSDN. 在MSDN上确实有一个例子。 http://msdn.microsoft.com/en-us/library/7tas5c80.aspx http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

Unfortunately there isn't such a Cell or Column Type you can use (as far as i know). 不幸的是,据我所知,没有可以使用的单元格或列类型。 But Microsoft provides an example on how to get a NumericUpDown (that also doesn't exist) into a DataGridView. 但是Microsoft提供了一个有关如何将NumericUpDown(也不存在)放入DataGridView 的示例

Maybe you can adopt this example to get your DateTimePicker into a DataGridView. 也许您可以采用此示例将DateTimePicker放入DataGridView中。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM