Data Filtering in Reports V2

The following data filtering approaches are available in Reports V2.

  1. FilterString Property of the XtraReport
  2. Criteria Property of the Data Source
  3. XtraReport Parameters
  4. Use the Parameters Object

1. FilterString Property of the XtraReport

The XtraReportBase.FilterString property specifies the filter applied on the client side before a report is displayed. The ellipsis button displayed next to the FilterString value in the Properties window invokes the FilterString Editor dialog, which simplifies filter creation.

XtraReport.FilterString

2. Criteria Property of the Data Source

The data source's DataSourceBase.Criteria property is used to set a filter on the server side. The ellipsis button displayed next to the Criteria value in the Properties window invokes the FilterString Editor dialog, which simplifies criteria creation.

ReportsV2_Filtering

You can also use the following code to modify criteria from Report Scripts.

private void xtraReport1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
    var report = (DevExpress.XtraReports.UI.XtraReport)sender;
    var dataSource = (DevExpress.Persistent.Base.ReportsV2.ISupportCriteria)report.DataSource;
    dataSource.Criteria = DevExpress.Data.Filtering.CriteriaOperator.Parse(
        "StartsWith(LastName, 'A')");
}

3. XtraReport Parameters

Report Parameters can participate in the Criteria. To refer to a parameter value, precede the parameter value with "?" (e.g., "[Name] = ?parameterName"). For additional information on using parameters in the Filter Builder, refer to the Data Filtering Overview topic.

4. Parameters Object

You can use a custom non-persistent class inherited from the ReportParametersObjectBase abstract class included in the Reports V2 module, instead of XtraReports parameters. This object's Detail View will be displayed in a popup dialog before a report is displayed. Filtering and sorting specified via this object's ReportParametersObjectBase.GetCriteria and ReportParametersObjectBase.GetSorting methods are applied to the data source. This approach is useful for defining complex filtering and sorting.

To add your Parameters Object to the Application Model and to adjust the object's Detail View layout in the Model Editor, apply the DomainComponentAttribute to the class' declaration.

using DevExpress.Xpo;
using DevExpress.Xpo.DB;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.ReportsV2;
// ...
[DomainComponent]
public class DemoParameters : ReportParametersObjectBase {
    public bool SortByFirstName { get; set; }
    public bool FilterByFirstName { get; set; }
    public string FirstName { get; set; }
    public bool FilterByPosition { get; set; }
    public Position ContactPosition { get; set; }

    public DemoParameters(IObjectSpaceCreator provider) : base(provider) {
        ContactPosition = ObjectSpace.FindObject<Position>(CriteriaOperator.Parse("Title == 'Developer'"));
    }

    protected override IObjectSpace CreateObjectSpace() {
        return objectSpaceCreator.CreateObjectSpace(typeof(Contact));
    }

    public override CriteriaOperator GetCriteria() {
        CriteriaOperator criteriaName = null;
        CriteriaOperator criteriaPosition = null;
        if (FilterByFirstName) {
            criteriaName = CriteriaOperator.Parse("FirstName = ?", FirstName);
        }
        if (FilterByPosition) {
            criteriaPosition = CriteriaOperator.Parse("Position.Oid = ?", ContactPosition.Oid);
        }
        return CriteriaOperator.And(criteriaName, criteriaPosition);
    }

    public override SortProperty[] GetSorting() {
        List<SortProperty> sorting = new List<SortProperty>();
        if (SortByFirstName) {
            sorting.Add(new SortProperty("FirstName", SortingDirection.Ascending));
        }
        return sorting.ToArray();
    }

    public override string ToString() {
        return "London";
    }
}

Associate the Report Parameters Object with the Report

  • If a report is created at runtime, you can associate the Report Parameters Object via the IReportDataV2.ParametersObjectType property. Right-click the report in the Reports list view and choose Edit. Then, choose the Parameters Object Type in the invoked Detail View.

    ReportsV2_ParametersObject

  • If a report is predefined (created at design time), the Parameters object type can be passed to the PredefinedReportsUpdater.AddPredefinedReport method.

    predefinedReportsUpdater.AddPredefinedReport<ContactsBaseReport>(
        "Contacts by Department", typeof(ContactsReport), typeof(MyParametersObject));
    

    For details on adding predefined reports, refer to the PredefinedReportsUpdater class description.

Tip