Home > Software design >  Convert values from DataGridView to List<DateTime>
Convert values from DataGridView to List<DateTime>

Time:02-22

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

CodePudding user response:

You need to convert dc.Value to dateTime.

item = Convert.ToDateTime(dc.Value)

  • Related