Back to Devexpress

How to: Modify Date Formatting of Headers in the XtraScheduler Report

windowsforms-6233-controls-and-libraries-scheduler-examples-date-and-time-how-to-modify-date-formatting-of-headers-in-the-xtrascheduler-report.md

latest3.5 KB
Original Source

How to: Modify Date Formatting of Headers in the XtraScheduler Report

  • Nov 13, 2018
  • 2 minutes to read

This document illustrates how the Scheduler services can be used in Scheduler Reporting to modify captions of the view elements. In other words, the default HeaderCaptionService is substituted to change the date formatting used by the HorizontalDateHeaders in the daily style report.

Note

You can handle the HorizontalDateHeaders.CustomDrawDayHeader event to accomplish manual painting of the header.

csharp
using DevExpress.XtraScheduler.Reporting;
using DevExpress.XtraScheduler.Services;
using DevExpress.XtraScheduler.Drawing;
using DevExpress.XtraScheduler.Services.Implementation;
// ...
    string dateFormat = "dd-MMM, ddd";
    SchedulerStoragePrintAdapter storagePrintAdapter = 
        XtraSchedulerReport1.SchedulerAdapter as SchedulerStoragePrintAdapter;
    if (storagePrintAdapter != null) {
        storagePrintAdapter.RemoveService(typeof(IHeaderCaptionService));
        IHeaderCaptionService customHeaderCaptionService = 
            new CustomHeaderCaptionService(dateFormat);
        storagePrintAdapter.AddService(typeof(IHeaderCaptionService), 
            customHeaderCaptionService);
    }

public class CustomHeaderCaptionService : HeaderCaptionServiceWrapper {
    string format;
    public CustomHeaderCaptionService(string format)
        : base(new HeaderCaptionService()) {
        this.format = format;
    }
    protected virtual string CreateFormat(string format) {
        if(format == "Default")
            return string.Empty;
        return String.Format("{{0:{0}}}", format);
    }
    public override string GetDayColumnHeaderCaption(DayHeader header) {
        return CreateFormat(format);
    }
}
vb
Imports DevExpress.XtraScheduler.Reporting
Imports DevExpress.XtraScheduler.Services
Imports DevExpress.XtraScheduler.Drawing
Imports DevExpress.XtraScheduler.Services.Implementation
' ...
Dim dateFormat As String = "dd-MMM, ddd"
Dim storagePrintAdapter As SchedulerStoragePrintAdapter = _
    TryCast(XtraSchedulerReport1.SchedulerAdapter, SchedulerStoragePrintAdapter)
If Not storagePrintAdapter Is Nothing Then
    storagePrintAdapter.RemoveService(GetType(IHeaderCaptionService))
    Dim customHeaderCaptionService As IHeaderCaptionService = _
        New CustomHeaderCaptionService(dateFormat)
    storagePrintAdapter.AddService(GetType(IHeaderCaptionService), customHeaderCaptionService)
End If

Public Class CustomHeaderCaptionService
    Inherits HeaderCaptionServiceWrapper
    Private format As String
    Public Sub New(ByVal format As String)
        MyBase.New(New HeaderCaptionService())
        Me.format = format
    End Sub
    Protected Overridable Function CreateFormat(ByVal format As String) As String
        If format = "Default" Then
            Return String.Empty
        End If
        Return String.Format("{{0:{0}}}", format)
    End Function
    Public Overrides Function GetDayColumnHeaderCaption(ByVal header As DayHeader) As String
        Return CreateFormat(format)
    End Function
End Class

See Also

Services

Formatting Services