public void GetItems(DataGridView dgvCalendar, DateTime date, CalendarViewType calendarViewType) { var startDate = date.GetStartDate(calendarViewType); dgvCalendar.ColumnCount = calendarViewType.GetNumberOfDays(date); SetRowHeaderCells(dgvCalendar); SetColumnHeaderCells(dgvCalendar, startDate); // ToDo: Fix performance issue. GetAll should be called with a predicate. var allMeetings = meetingRepository.GetAll(); var allEvents = eventRepository.GetAll(); for (int dayIndex = 0; dayIndex < dgvCalendar.ColumnCount; dayIndex++) { var actualDate = startDate.AddDays(dayIndex); var meetings = allMeetings.Where(meeting => meeting.MeetingDate.IsRepeatedOnDate(actualDate, (RepetitionType)meeting.RepetitionType, meeting.RepetitionValue, meeting.ExpirationDate)).ToList(); var events = allEvents.Where(myEvent => myEvent.EventDate.IsRepeatedOnDate(actualDate, (RepetitionType)myEvent.RepetitionType, myEvent.RepetitionValue, myEvent.ExpirationDate)).ToList(); var slotStartTime = new TimeSpan(0, 0, 0); for (int i = 0; i < 48; i++) { var slotEndTime = slotStartTime.Add(HalfAnHour); var slotMeetings = meetings .Where(meeting => DateTimeExtensions.AreDatesTimeBetweenTimeSpans(meeting.MeetingDate, meeting.MeetingEndDate, slotStartTime, slotEndTime)) .Select(meeting => meeting.Title); var slotEvents = events .Where(myEvent => DateTimeExtensions.AreDatesTimeBetweenTimeSpans(myEvent.EventDate, myEvent.EventEndDate, slotStartTime, slotEndTime)) .Select(myEvent => myEvent.Title); var meetingsText = String.Join(", ", slotMeetings); var eventsText = String.Join(", ", slotEvents); var slotText = $"{meetingsText};{eventsText}".Trim(';'); dgvCalendar.Rows[i].Cells[dayIndex].Value = slotText; slotStartTime = slotEndTime; } } }