Search results

Report parameters

Provides property options to pass or set report parameters default values at run time using the parameters property. You can set the report parameters while creating the Report Viewer control in a script or in the Web API Controller.

In this tutorial, the sales-order-detail.rdl report is used, and it can be downloaded from here.

Set parameter at client

To set parameter default value in the client side, The following code example illustrates how to set report parameter,

@(Html.Bold().ReportViewer("viewer")
    .ReportPath("~/Resources/sales-order-detail.rdl")
    .ReportServiceUrl("/api/ReportViewer")
    .Parameters(param => { param.Name("SalesOrderNumber").Labels(new List<string>() { "SO50755" }).Values(new List<string>() { "SO50755" }).Add(); })
)

Set parameters in Web API Controller

To set parameter default value in Web API Controller, use the following code in the OnReportLoaded method.

public void OnReportLoaded(ReportViewerOptions reportOption)
{
    List<BoldReports.Web.ReportParameter> userParameters = new List<BoldReports.Web.ReportParameter>();
    userParameters.Add(new BoldReports.Web.ReportParameter()
    {
        Name = "SalesOrderNumber",
        Values = new List<string>() { "SO50755" }
    });
    reportOption.ReportModel.Parameters = userParameters;
}

The Report Parameters name should be case sensitive

Get report parameter

The ReportHelper class provides methods to get the report parameters used in the report. The following helper methods used to get parameter with or without values.

Methods Description
GetParameters Returns the parameters used in the current report without the processed values.
GetParametersWithValues Returns the report parameters with processed data values that are used in the current report.

You can use the following code sample to get parameter names and set parameter default values.

public class ReportViewerController : ApiController, IReportController
{
    Dictionary<string, object> jsonArray = null;

    public object PostReportAction(Dictionary<string, object> jsonResult)
    {
        jsonArray = jsonResult;
        return ReportHelper.ProcessReport(jsonResult, this);
    }

    [System.Web.Http.ActionName("GetResource")]
    [AcceptVerbs("GET")]
    public object GetResource(string key, string resourcetype, bool isPrint)
    {
        return ReportHelper.GetResource(key, resourcetype, isPrint);
    }

    public void OnInitReportOptions(ReportViewerOptions reportOption)
    {
        // You can update report options here
    }

    public void OnReportLoaded(ReportViewerOptions reportOption)
    {
        var reportParameters = ReportHelper.GetParameters(jsonArray, this);
        List<BoldReports.Web.ReportParameter> setParameters = new List<BoldReports.Web.ReportParameter>();

        if (reportParameters != null)
        {
            foreach (var rptParameter in reportParameters)
            {
                setParameters.Add(new BoldReports.Web.ReportParameter()
                {
                    Name = rptParameter.Name,
                    Values = new List<string>() { "SO50755" }
                });
            }

            reportOption.ReportModel.Parameters = setParameters;
        }
    }
}

Change the Parameter drop-down height and width

The parameterSettings helps you to change the height and width of the parameter available in parameter panel.

@(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
     .ParameterSettings(parameter => parameter.PopupHeight("100px").PopupWidth("150px"))
)

Hide a Parameter scroller

The enableparameterblockscroller helps you to hide the scrollbar in parameter panel.

@(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
    .EnableParameterBlockScroller(false)
)

Hide a Parameter Pane on load

The parameterSettings helps you to hide and show the parameter block.

@(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
    .ParameterSettings(parameter => parameter.HideParameterBlock(true))
)

Change the Parameter Item Width and Label Width

The parameterSettings helps you to change the parameter Item width and label width.

@(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
    .ParameterSettings(parameter => parameter.ItemWidth("250px").LabelWidth("auto"))
)

Set date range for date parameter

The properties MaxDateTime and MinDateTime in the parameterSettings are used to set date range for date parameters in a report.

Property Description
MaxDateTime Specify minimum range value of a date parameter
MinDateTime Specify maximum range value of a date parameter

Refer to the following code sample to set data range using parameterSettings in Report Viewer controller.

        public ActionResult Index()
        {
            ViewBag.parameterSettings = new BoldReports.Models.ReportViewer.ParameterSettings();
            ViewBag.parameterSettings.MaxDateTime = new DateTime(2003, 8, 22);
            ViewBag.parameterSettings.MinDateTime = new DateTime(2003, 4, 22);
            return View();
        }

Refer to the following code sample to set data range to the report viewer initialization.

   @(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
    .ReportPath("~/Resources/sales-order-detail.rdl")
    .ParameterSettings(parameterSettings => parameterSettings.MaxDateTime(@ViewBag.parameterSettings.MaxDateTime))
    .ParameterSettings(parameterSettings => parameterSettings.MinDateTime(@ViewBag.parameterSettings.MinDateTime))
)

The above code sets date range for all the date parameters used in the report.

To set different date range for each date parameter used in the report, register the event beforeParameterAdd and specify range based on parameter name as in below code sample.

   @(Html.Bold().ReportViewer("viewer")
    .ReportServiceUrl("/api/ReportViewer")
    .ReportPath("~/Resources/product-line-sales.rdl")
    .BeforeParameterAdd("beforeParameterAdd")
    )
    <script type="text/javascript">
        function beforeParameterAdd(args) {
            if (args.parameterModel.Name === "StartDate") {
                args.parameterSettings.minDateTime = new Date("4/5/2003 5:00:00 AM");
                args.parameterSettings.maxDateTime = new Date("4/15/2003 5:00:00 AM");
            }
            if (args.parameterModel.Name === "EndDate") {
                args.parameterSettings.minDateTime = new Date("5/10/2003 5:00:00 AM");
                args.parameterSettings.maxDateTime = new Date("5/20/2003 5:00:00 AM");
            }
        }
    </script>