Using Report Parameters Programmatically
In the report viewing application you can populate the values of ReportParameters collection members prior to displaying the report.
Report1 report = new Report1();
report.ReportParameters["ManagerID"].Value = "123";
Dim report As New Report1()
report.ReportParameters("ManagerID").Value = "123"
At runtime you can access the report parameters through the Telerik.Reporting.Processing.Report.Parameters dictionary. Each Parameter object contains resolved available values, current value and label. The label returns the currently selected DisplayMember from the available values (if available values are defined).
Prior to version 2010 Q1 the processing report exposes a dictionary containing only the current values of the report parameters.
Example:
- Create a Telerik Report
- Use the instructions from the How to Connect to a SQL Database article to bind to the uspGetManagerEmployees stored procedure from the AdventureWorks table.
- Leave the Value for the @ManagerID data source parameter empty and finish the wizard.
-
Create the following layout for the report
Add a report parameter and name it ManagerID.
- Change its Type to Integer.
- Set its Visible property to True.
-
Expand the AvailableValues and bind it to SqlDataSource component with the following query:
SELECT M.ManagerID, C.FirstName + ' ' + C.LastName AS Name FROM (SELECT DISTINCT ManagerID FROM HumanResources.Employee) AS M INNER JOIN HumanResources.Employee AS E ON M.ManagerID = E.EmployeeID INNER JOIN Person.Contact AS C ON E.ContactID = C.ContactID
- Set the ValueMember to = Fields.ManagerID.
- Set the DisplayMember to = Fields.Name.
- Add grouping for the report with = Fields.ManagerID as grouping expression.
- Wire the NeedDataSource event of the report.
- In the report.cs file set the DataSource property to null ( Nothing in VB.NET) so that NeedDataSource event is fired.
-
Add the following code to the NeedDataSource event handler:
private void Report1_NeedDataSource(object sender, System.EventArgs e) { //Take the Telerik.Reporting.Processing.Report instance Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender; // Transfer the value of the processing instance of ReportParameter // to the parameter value of the sqlDataSource component this.sqlDataSource1.Parameters[0].Value = report.Parameters["ManagerID"].Value; // Set the SqlDataSource component as it's DataSource report.DataSource = this.sqlDataSource1; }
Private Sub Report1_NeedDataSource(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.NeedDataSource 'Take the Telerik.Reporting.Processing.Report instance Dim report As Telerik.Reporting.Processing.Report = DirectCast(sender, Telerik.Reporting.Processing.Report) ' Transfer the value of the processing instance of ReportParameter ' to the parameter value of the sqlDataSource component Me.sqlDataSource1.Parameters(0).Value = report.Parameters("ManagerID").Value ' Set the SqlDataSource component as it's DataSource report.DataSource = Me.sqlDataSource1 End Sub
- Display the Report in a report viewer.
-
Select a parameter from the available values in the parameter editor and click Preview.