Blogs

Blogs

DevExpress XtraReports – Filtering Subreports Via XtraReports Scripts

Here at PSP IT Design and Development we create many DevExpress XAF projects which include embedded reports for the end users of the application, due to the complicated nature of these reports we create them here at PSP and then include them as part of the project which automatically update whenever we undertake a new release of the application.

Some of these reports contain one or many sub reports, which then need to be filtered based on the parameters inputted into the main report, this is useful when you wish to report from two or more different Data Types in one report but share the same input parameters. Below are the steps on embedding a sub report within a main report and then using the parameters from this main report to filter the sub report.

First of all you will need to create the initial main report and style this as required along with adding any required parameters.

Next you will have to create the sub report and style this as required, you do not need to duplicate the parameters from the main report into the sub report.

You will then have to embed this sub report into the main report.

Next you will have to write the script which will filter the sub report based on the main reports parameters. Please see the basic code example below, this can be added too and expanded into more complex filter strings.

using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Reports;

private void SubReportOne_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
{
      BusinessObject parameterOne = ((XafReport)MainReportOne).Parameters["ParameterOne"].Value as BusinessObject;
      DevExpress.ExpressApp.Reports.XafReport subreport = ((XRSubreport)sender).ReportSource as DevExpress.ExpressApp.Reports.XafReport;
      subreport.BeforePrint += new PrintEventHandler(delegate(object sender2, System.Drawing.Printing.PrintEventArgs e2)
      {
            IList originalDataSource = null;

            if (parameterOne != null)
            {
                  CriteriaOperator criteria = CriteriaOperator.Parse("FieldOne.Oid=?”, parameterOne.Oid);
                  originalDataSource = subreport.ObjectSpace.GetObjects(subreport.DataType, criteria);
            }
            else
            {
                  originalDataSource = subreport.ObjectSpace.GetObjects(subreport.DataType);
            }

            subreport.DataSource = new ProxyCollection(subreport.ObjectSpace, DevExpress.ExpressApp.XafTypesInfo.Instance.FindTypeInfo(subreport.DataType), originalDataSource);
      });
}

Obviously if necessary you can add more than one parameter and expand the Criteria Operator filter as required and add more null checks to do different filtering functions if certain parameters are null.

You now have to assign the newly created script to activate Before Print on the subreport.

Now when you run the main report the sub report will also be filtered based on the parameters selected and the filter string you used for the Criteria Operator.

This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.