user2023203
user2023203

Reputation: 556

C# putting datagridview date in a datetimepicker

I have this method:

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

column number 3 has a datetime value in it and i want to put this date in a datetimepicker. how do I do that?

Upvotes: 0

Views: 4001

Answers (1)

user2480047
user2480047

Reputation:

By assuming that this cell contains a valid (string) date in the format expected by your current culture, you can do this:

dateTimePicker1.Value = Convert.ToDateTime(row.Cells[3].Value.ToString());

If you are not sure if the given cell contains a valid (string) date, you can use TryParse:

DateTime curDate;
if (DateTime.TryParse(row.Cells[3].Value.ToString(), out curDate))
{
    dateTimePicker1.Value = curDate;
}

Upvotes: 1

Related Questions