Skip to main content

Date Range Report Parameters

  • 7 minutes to read

This topic describes how to create a date range parameter and filter a report’s data by the specified dates.

Create a Date Range Parameter in the Report Designer

Follow the steps below to add a date range parameter to a report in the Visual Studio Report Designer:

  1. Create a report parameter and set the Value Source option (which corresponds to the parameter’s ValueSourceSettings property) to RangeParametersSettings. The Start Parameter and End Parameter sections appear, and you can configure options in these sections to create a date range.

    add-parameter-dialog

  2. Set the name and initial value for the Start Parameter and End Parameter. To specify an expression instead of a static value, click the Value option’s ellipsis button and use the Expression Editor dialog.

    value-expression

After you create a date range parameter, you can reference the names of the Start Parameter and End Parameter in the report’s FilterString to filter the report’s data by the created date range. Select the report, click the FilterString‘s ellipsis button in the Properties window, and construct a filter condition in the invoked FilterString Editor.

filterstring

When you switch to the report’s Print Preview tab, the Parameters Panel displays the date range parameter. After you submit a start and end date, the report document shows filtered data.

The start and end parameter values store the selected day’s midnight time. For instance, if you choose 10/15/2023, the DateTime value is 10/15/2023 12:00:00 AM. If your date fields include non-midnight times, records for the end date 10/15/2023 are excluded from the report. To include data for the 10/15/2023 date, use the GetDate() function in the FilterString Editor.

filterstring

Create a Date Range Parameter in Code

  1. Initialize a RangeParametersSettings class instance and set up its properties.
  2. Create a parameter and assign the created RangeParametersSettings object to the parameter’s ValueSourceSettings property.
  3. Add the parameter to the report’s Parameters collection.

The code sample below demonstrates how to create a date range parameter in code and use this parameter to filter the report’s data.

View Example: Create a Date-Range Report Parameter

using System;
using System.IO;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.Expressions;
using DevExpress.XtraReports.Parameters;
using DevExpress.XtraReports.UI;
// ...
            // Create a date range parameter.
            var dateRangeParam = new Parameter();
            dateRangeParam.Name = "dateRange";
            dateRangeParam.Description = "Date Range:";
            dateRangeParam.Type = typeof(System.DateTime);

            // Create a RangeParametersSettings instance and set up its properties.
            var dateRangeSettings = new RangeParametersSettings();

            // Specify the start date and end date parameters.
            dateRangeSettings.StartParameter.Name = "dateRangeStart";
            dateRangeSettings.StartParameter.ExpressionBindings.Add(
                new BasicExpressionBinding("Value", "AddYears(Today(), -1)")
            );

            dateRangeSettings.EndParameter.Name = "dateRangeEnd";
            dateRangeSettings.EndParameter.ExpressionBindings.Add(
                new BasicExpressionBinding("Value", "AddYears(Today(), 0)")
            );

            // Assign the settings to the parameter's ValueSourceSettings property.
            dateRangeParam.ValueSourceSettings = dateRangeSettings;

            // Create a report instance and add the parameter to the report's Parameters collection.
            var report = new XtraReport1();
            report.Parameters.Add(dateRangeParam);

            // Use the parameter to filter the report's data.
            report.FilterString = "GetDate([UpdatedOrderDate]) Between(?dateRangeStart,?dateRangeEnd)";

Customize Predefined Ranges

The date range parameter editor allows you to select a range from a list of predefined ranges. You can remove items from the list, or create a custom predefined range and add it to the list.

WinForms and WPF Reporting

The set of predefined date ranges is stored in the RangeParameterEditorOptions static class and is available in all date range parameter editors when they are displayed in the Preview tab.

predefined-date-ranges

The code sample below demonstrates how to create two predefined date ranges.

using DevExpress.XtraReports.Parameters;
using DevExpress.XtraReports.Expressions;
// ...
// Remove all predefined ranges and add two new date ranges.
RangeParameterEditorOptions.PredefinedDateRanges.Clear();
RangeParameterEditorOptions.RegisterDateRange("September", () => new DateTime(2019,9,1), () => new DateTime(2019,9,30));
RangeParameterEditorOptions.RegisterDateRange("October", () => new DateTime(2019,10,1), () => new DateTime(2019,10,31));

Web Reporting

Predefined date ranges are stored in the client-side collection of the Document Viewer widgets. The set of predefined date ranges is available in all date range parameter editors when they are displayed in the Preview tab.

web-predefined-date-ranges

The following code removes built-in predefined ranges and adds two custom predefined ranges.

<script type="text/javascript" id="script">
    function CustomizePredefinedRanges(s, e) {
        DevExpress.Reporting.Viewer.Widgets.predefinedDateRanges.splice(0,
            DevExpress.Reporting.Viewer.Widgets.predefinedDateRanges.length);

        DevExpress.Reporting.Viewer.Widgets.predefinedDateRanges.push({
            displayName: 'September',
            range: () => [
                new Date(2023, 8, 1),
                new Date(2023, 8, 30)
            ]
        })
        DevExpress.Reporting.Viewer.Widgets.predefinedDateRanges.push({
            displayName: 'October',
            range: () => [
                new Date(2023, 9, 1),
                new Date(2023, 9, 31)
            ]
        })
    }
</script>

<dx:ASPxWebDocumentViewer ClientInstanceName="WebDocumentViewer1" ID="ASPxWebDocumentViewer1" runat="server">
    <ClientSideEvents BeforeRender ="CustomizePredefinedRanges" />
</dx:ASPxWebDocumentViewer>
See Also