Ebosin
Ebosin

Reputation: 35

Convert values from DataGridView to List<DateTime>

I need to convert values from DataGridView to List (dd-mm-yyyy). To Add values to DataGridView, I use DateTimePicker and Button. On Button_Click:

DateTime dt = datetimepicker1.Value.Date;
RowsWithDates.Rows.Add(dt.ToString("d"));

Now i want to add all dates from RowsWithDates (DataGridView) to List. I tried this, but without success.

List<DateTime> items = new List<DateTime>();
foreach (DataGridViewRow dr in RowsWithDates.Rows)
{
    DateTime item = new DateTime();
    foreach (DataGridViewCell dc in dr.Cells)
    {
        item = dc.Value;//here i had error (can't convert object to System.DateTime)
    }
    items.Add(item);
}

Upvotes: 0

Views: 57

Answers (1)

Darren_D19
Darren_D19

Reputation: 121

You need to convert dc.Value to dateTime.

item = Convert.ToDateTime(dc.Value)

Upvotes: 1

Related Questions