Filter Report Data Source using the ParametersObjectType Property

In real world business reports, you may need to utilize a filtered data source for the report content. For example, you may need to have information on employees from the R&QA department only. The applications built via the eXpressApp Framework provide the capability to design such reports. There are several techniques for filtering a report data source. This topic details one of them - using the report's ParametersObjectType property to set a filter. To learn about the other methods, refer to the Filter Report Data Source using XtraReports Parameters and Filter Report Data Source using the Filter Property topics.

Specify the Report's ParametersObjectType Property

To filter a report data source, run the Windows Forms application and invoke the Report Designer for the required report. Click on the report design area and locate the Filtering property section in the Property Grid. This section contains several properties including the ParametersObjectType property. By default, it is set to none, which means that this property is not in effect.

Reports_FilterProperty_1

Except for the none value, this property can be set to a custom descendant of the ReportParametersObjectBase class. This class provides the GetCriteria and GetSorting methods. They are called when previewing the report. In your ReportParametersObjectBase class descendant, you can override these methods to return custom criteria or/and sorting for the report data source.

To add a ReportParametersObjectBase class descendant, open the Template Gallery for the ASP.NET application project and choose the XAF Report Parameters Objects | XAF Report Parameters Object project item.

TemplateGalery_ReportParametersObject

As a result, you will get an automatically generated code file with a single class declaration. The following code demonstrates how to override the GetCriteria and GetSorting methods of this class.

[DomainComponent]
public class MyReportParameters : ReportParametersObjectBase {
   public MyReportParameters(IObjectSpace objectSpace, Type reportDataType) : 
      base(objectSpace, reportDataType) { }
   public override CriteriaOperator GetCriteria() {
      return CriteriaOperator.Parse("FirstName Like ?", "A%");
   }
   public override SortProperty[] GetSorting() {
      SortProperty[] sorting = { 
         new SortProperty("FullName", SortingDirection.Ascending) };
      return sorting;
   }
}

This class will be used for Person objects. The data source for a report that uses this class will contain only objects whose FirstName property value begins with "A". In addition, these objects will be sorted by the FullName property values in ascending order.

After implementing the code above, you can select the MyReportParameters type in the dropdown list of the ParametersObjectType property in the Report Designer's Property Grid:

Reports_ParametersObjectTypeProperty_2

When viewing the report via the ExecuteReport Action, the report will be filtered and sorted according to the criteria and sorting specified in the ReportParametersObject1 type. Note, that a filter specified via the ParametersObjectType property has no effect when previewing a report via the Report Designer.

Note

If you set the ParametersObjectType property to a ReportParametersObjectBase class descendant (not to none), the Filter and FilterDescription properties will not be considered.

Set User-Oriented Criteria and Sorting

You can allow users to specify criteria values or sort settings. To do this, add properties to your descendant and use them in the criteria. In this instance, a detail form with corresponding property editors will be invoked when trying to preview the report.

The following code demonstrates how to specify user-oriented criteria.

[DomainComponent]
public class MyReportParameters : ReportParametersObjectBase {
    public MyReportParameters(IObjectSpace objectSpace, Type reportDataType) : 
          base(objectSpace, reportDataType) { }
    public override CriteriaOperator GetCriteria() {
        return CriteriaOperator.Parse("Address1.City = ?", City);
    }
    public override SortProperty[] GetSorting() {
        if (SortByFullName)
            return new SortProperty[] { new SortProperty("FullName", SortingDirection.Ascending) };
        else
            return new SortProperty[0];
    }
    public bool SortByFullName { get; set; }
    public string City { get; set; }
}

If you set the above class for a report's ParametersObjectType property, the following detail form will be invoked when previewing this report:

Reports_ParametersObjectTypeProperty_3

Note

To see more examples on how to implement ReportParametersObject classes, refer to the Reports section in the Feature Center Demo shipped with XAF installation, or refer to the Feature Center demo online.

Approach Advantages and Disadvantages

Advantages:

  • You can set filters that are quite complex. The filters that cannot be set via the Filter Builder, or manually, specifying the Filter property, can be set in the ReportParametersObjectBase.GetCriteria method.
  • Report data can be sorted by implementing the ReportParametersObjectBase.GetSorting method.
  • You can use the same ReportParametersObjectBase class descendants for different reports. To do this, use the ReportParametersObjectBase.ReportDataType property. It specifies the type of objects that form the report data source (see Use Function Criteria Operators to Filter Report Data Source).

Disadvantages:

See Also