Telerik blogs

An interesting question that we frequently are being asked is: How to create a calendar report? Generally, the requirement is to create a report showing appointments and bookings just as in a month planner calendar/scheduler. When you think about it, tracking tasks and events when displayed in a calendar is instant - the users have all the information they need into a well known layout that allows to easily analyze tasks and projects. It is no wonder that our clients request this type of report. The easiest way to prepare a dynamic calendar report is to utilize our flexible crosstab/table/list item.


Due to the crosstabs data driven nature the calendar is generated thoroughly on CalendarDataSource data.

public class CalendarDataSource
    {
        public IEnumerable<DateTime> GetDaysBetween(DateTime startDate, DateTime endDate)
        {
            // truncate the time part
            var date = startDate.Date;
            endDate = endDate.Date;
 
            while (date <= endDate)
            {
                yield return date;
                date = date.AddDays(1);
            }
        }
    }

To format the crosstab in calendar format we have used a few helper user functions:

public static DateTime GetFirstDayOfMonth(DateTime value)
       {
           return new DateTime(value.Year, value.Month, 1);
       }
 
       public static DateTime GetLastDayOfMonth(DateTime value)
       {
           return new DateTime(ReportingCalendarvalue.Year, value.Month, 1).AddMonths(1).AddDays(-1);
       }
        
       public static int GetWeekOfYear(DateTime value)
       {
           var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
           return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(value, CalendarWeekRule.FirstDay, firstDayOfWeek);
       
      
       public static int GetDayOfWeek(DateTime value)
       {
           var firstDayOfWeek = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
           var dayOfWeek = (int)value.DayOfWeek;
 
           return  (dayOfWeek + 7 - firstDayOfWeek) % 7;
       }
 
       public static string GetDayOfWeekName(DateTime value)
       {
           var dayNames = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
           return dayNames[(int)value.DayOfWeek];
       }
 
       public static bool IsWeekend(DateTime value)
       {
           return (value.DayOfWeek == DayOfWeek.Sunday || value.DayOfWeek == DayOfWeek.Saturday);
       }


The appointments are displayed in a subreport item. The labels of the appointments show two fields, task name and time. However you can add any kind of field that is required simply by adding a textbox to the subreport.The datasource is generated random data. You can play around with the RandomEventGenerator class that is included in the attached sample project. 
Here is how the calendar report looks like:

ReportingCalendarC#

ReportingCalendarVB.zip


About the Author

Stefan Tsokev

Stefan’s main interests outside the .NET domain include rock music, playing the guitar and swimming.

Comments

Comments are disabled in preview mode.