我有这个方法:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
textBox1.Text = row.Cells[0].Value.ToString();
textBox2.Text = row.Cells[1].Value.ToString();
textBox3.Text = row.Cells[2].Value.ToString();
dateTimePicker1 = row.Cells[3];
}
}
第 3 列中有一个日期时间值,我想将此日期放在日期时间选择器中。 我该怎么做?
通过假定此单元格包含当前区域性所期望格式的有效(字符串)日期,您可以执行以下操作:
dateTimePicker1.Value = Convert.ToDateTime(row.Cells[3].Value.ToString());
如果您不确定给定单元格是否包含有效的(字符串)日期,则可以使用 TryParse
:
DateTime curDate;
if (DateTime.TryParse(row.Cells[3].Value.ToString(), out curDate))
{
dateTimePicker1.Value = curDate;
}