1

I have a large datatable of values and a DateTime. There should be a row for every five minutes of a day. The problem is that there are rows missing. How would I go about creating rows to fill in the gaps so that the DataTable is complete?

Amheus
  • 21
  • 3
  • 2
    Wouldn't it make more sense to handle those gaps when querying / using the data. You don't tell what data there is with timestamps. But filling just timestamp and leaving all other fields NULL doesn't make any sense to me. – ex4 Apr 25 '20 at 14:49
  • As per @ex4, when fetching the data, place it e.g. into an array. Then, run a simple loop over every 5 mins of the day incrementing from a new `DateTime` in 5 minute chunks. If there is a record in your array, `continue`; otherwise, create it. – zaitsman Apr 25 '20 at 14:52
  • Or you can even do it when querying data from database. See https://stackoverflow.com/questions/5002661/how-to-group-time-by-hour-or-by-10-minutes – ex4 Apr 25 '20 at 15:09
  • @zaitsman I've attempted to iterate through the datatable and create rows where there should be rows and it works to a point but then all of the results are just repeated at the end, I've added the code in the comment after this: – Amheus Apr 25 '20 at 15:21
  • `DateTime dateTime = DateTime.Parse("2020-04-25 00:00:00.0000000"); for (int i = 0; i <= (1440 / 5); i ++) { if (System.Convert.ToDateTime(table_today.Rows[i][0].ToString()) != dateTime) { DataRow nullRow = table_today.NewRow(); nullRow[0] = dateTime; nullRow[1] = new int(); nullRow[2] = new int(); nullRow[3] = new int(); table_today.Rows.InsertAt(nullRow, i); } dateTime = dateTime.AddMinutes(5); }` – Amheus Apr 25 '20 at 15:21

0 Answers0