How to pass parameters to reports using Infragistics Reporting with WCF RIA Services and Client-Side Rendering

[Infragistics] Mihail Mateev / Friday, November 25, 2011

Client-side rendering is a great feature of Infragistics Reporting.

When using this approach in the Silverlight application it is not possible to create a database connection. Usually in similar cases are used  references to WEB services. This approach is related to an additional implementation of the method, which returns us a collection of type IList <T>.

This case is described in the article "Implementing Client-Side Rendering for Infragistics Reporting". Implementing a  WEB service, using data from the database also requires some effort. In this case it is more efficient to use an ORM as ADO.Net Entity Framework, and combine it with WCF RIA Services.

For example we will use ADO.Net Entity Framework and Domain Service Class, added to WEB project of the Silverlight application.

 

Silverlight client applications cannot access the database directly, which implies the report needs to load the data from somewhere else. This could be a web service or some data stored in a collection.

 

Domain Service Class Settings:

Let’s add to Domain Service Class instance a public method with parameter. In this sample is used entity Customers, generated from Northwind sample database. The method GetCustomersbyCountry(string country) returns customers filtered by country.

   1: public IQueryable<Customer> GetCustomersbyCountry(string country)
   2: {
   3:     if (country.Equals(string.Empty))
   4:     {
   5:         return GetCustomers();
   6:     }
   7:  
   8:     return GetCustomers().Where(c => c.Country == country);
   9: }
  10:     }

It is not possible in the client to select this method as a data source. Just style the report and do not care about report parameters.

 

Fortunately in this case we can use in the client application method GetCustomersbyCountry in the instance of the class DomainDataSource, which is used with WCF RIA Services.

DomainDataSource  and DomainContext Settings:

From the “All Silverlight Controls” group in the Toolbox, drag the DomainDataSource control to the XAML code. The DomainDataSource is provided by WCF RIA Services to retrieve and edit data through the specified domain context. For further details, see DomainDataSource on MSDN.

Modify DomainDataSource instance to use GetCustomersbyCountry method: for QueryName set “GetCustomersbyCountryQuery”.  Specify the domain context (NwDomainContext) for the DomainDataSource and add a query parameter, bound to a TextBox.

   1: <riaControls:DomainDataSource x:Name="domainDataSource" 
   2:                               AutoLoad="False" 
   3:                               QueryName="GetCustomersbyCountryQuery">
   4:  
   5:     <riaControls:DomainDataSource.DomainContext>
   6:         <Web:NwDomainContext />
   7:     </riaControls:DomainDataSource.DomainContext>
   8:     <riaControls:DomainDataSource.QueryParameters>
   9:         <riaControls:Parameter ParameterName="country" Value="{Binding ElementName=countryTextBox, Path=Text}" />
  10:     </riaControls:DomainDataSource.QueryParameters>
  11: </riaControls:DomainDataSource>

 

Add a button to load data in DomainDataSource using WCF RIA Services.

   1: <Button Command="{Binding Path=LoadCommand, ElementName=domainDataSource}" 
   2: Content="Load" Margin="5"  Width="100"  Style="{StaticResource LoadButton}" 
   3: Name="customerDomainDataSourceLoadButton" />

 

Bind the Report Viewer to the DomainDataSource. To do this, using the DataSource tag from ClientRenderSettings tag.

The code below shows a DomainDataSource that retrieves data from a domain context (NwDomainContext) and displays the data in a report (Customer) rendered in the Report Viewer control.

 

   1: <ig:XamReportViewer Grid.Row="1"  Margin="20" Name="xamReportViewer1">
   2:     <ig:XamReportViewer.RenderSettings>
   3:         <ig:ClientRenderSettings DefinitionUri="/ReportingWithWcfRia;component/RiaReport.igr">
   4:             <ig:ClientRenderSettings.DataSources>
   5:                 <!-- Overrides Order_Detail data source defined at design time-->
   6:                 <ig:DataSource TargetDataSource="Customer" ItemsSource="{Binding ElementName=domainDataSource}" />
   7:             </ig:ClientRenderSettings.DataSources>
   8:         </ig:ClientRenderSettings>
   9:     </ig:XamReportViewer.RenderSettings>
  10: </ig:XamReportViewer> 

 

Report Viewer Update

That is almost everything. You should be sure that the report viewer will be updated when the new data is loaded. In the DomainDataSource instance attach to the event LoadedData a new event handler, named DomainDataSourceLoadedData. In this method call method RenderReport() of the event viewer. This method will update your viewer with the actual data.

   1: private void DomainDataSourceLoadedData(object sender, LoadedDataEventArgs e)
   2: {
   3:     xamReportViewer1.RenderReport();
   4: }

 

 

   1: <riaControls:DomainDataSource x:Name="domainDataSource" LoadedData="DomainDataSourceLoadedData"
   2:                               AutoLoad="False" 
   3:                               QueryName="GetCustomersbyCountryQuery">
   4:  
   5:     <riaControls:DomainDataSource.DomainContext>
   6:         <Web:NwDomainContext />
   7:     </riaControls:DomainDataSource.DomainContext>
   8:     <riaControls:DomainDataSource.QueryParameters>
   9:         <riaControls:Parameter ParameterName="country" Value="{Binding ElementName=countryTextBox, Path=Text}" />
  10:     </riaControls:DomainDataSource.QueryParameters>
  11: </riaControls:DomainDataSource>

 

Run the application. When the TextBox for selected country is empty you will see the whole data.

Add for a selected country “Mexico” and press “Load” button. The report viewer will display only customers from Mexico

 

Enjoy! You could use WCF RIA Services to implement filtering for reports with client-side rendering. More details about how to work With RIA Services in Silverlight Using client-Side rendering you could find here.

Expect next articles bout Infragistics Reporting where you will learn how to use Infragistics Reporting with Visual Studio LightSwitch.

Source code you could download here: