private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { DataGridView dgv = sender as DataGridView; EntityExample entity = dgv.Rows[e.RowIndex].DataBoundItem as EntityExample; e.CellStyle.BackColor = entity.RowColor; }
private List <EntityExample> GenerateEntities(DateSpan span, TimeZoneInfo tz, DateTime dstHour) { var adjusted = false; Color nextColor = Color.White; var result = new List <EntityExample>(); DateTime start = span.Start; DateTime end = span.End; while (start < end) { var entity = new EntityExample() { ImportEffectiveDateStart = start, ImportEffectiveDateEnd = start.AddHours(1), RowColor = nextColor }; nextColor = Color.White; start = start.AddHours(1); if (IsSourceDataAdjustedDST) { if (entity.ImportEffectiveDateEnd == dstHour) { if (IsSpringForward) { entity.RowColor = Color.Yellow; entity.ImportEffectiveDateEnd = entity.ImportEffectiveDateEnd.AddHours(1); } else { if (!adjusted) { entity.ImportEffectiveDateEnd = entity.ImportEffectiveDateEnd.AddHours(-1); start = start.AddHours(-1); //repeat the hour adjusted = true; entity.RowColor = Color.Yellow; nextColor = Color.Red; } } } if (entity.ImportEffectiveDateEnd == dstHour.AddHours(-1)) { if (!IsSpringForward) { if (!adjusted) { entity.RowColor = Color.Yellow; } } } if (entity.ImportEffectiveDateStart == dstHour) { if (IsSpringForward) { nextColor = Color.Yellow; continue; //skip the hour } } } else { if (IsSpringForward) { if (entity.ImportEffectiveDateStart == dstHour) { entity.RowColor = Color.Yellow; } } else { if (entity.ImportEffectiveDateEnd == dstHour) { entity.RowColor = Color.Yellow; } } } var importDateTimeInfo = entity.ImportEffectiveDateStart.ParseImportDateTimeInfo(tz, ContractHour, IsSourceDataAdjustedDST); entity.UtcEffectiveDateStart = importDateTimeInfo.DateTimeUtc; entity.UtcEffectiveDateEnd = entity.ImportEffectiveDateEnd.ToUtc(tz, IsSourceDataAdjustedDST); entity.ContractHour = importDateTimeInfo.ContractDateTimeInfo.ContractHour; entity.ContractDay = importDateTimeInfo.ContractDateTimeInfo.ContractDay; entity.ContractMonth = importDateTimeInfo.ContractDateTimeInfo.ContractMonth; entity.DisplayEffectiveDateStart = entity.UtcEffectiveDateStart.ToSpecified(tz); entity.DisplayEffectiveDateEnd = entity.UtcEffectiveDateEnd.ToSpecified(tz); result.Add(entity); } return(result); }