Using Parameters and Scripts for Managing Columns Visibility

In order to manage columns visibility in a report using the code, it is needed:

To declare the parameters in the template (the Document.Parameters property collection).

To write the script managing the objects visibility in the report according to the passed parameters: the objects visibility management – Object.Visible Binding; management of the objects position – the Document.GenerateScript event handler.

To pass the parameters which include the data about visible columns to a template from the Silverlight application (method ReportViewer.Parameters.Add("<ParameterName>", <ParameterValue>).

Further we will examine the implementation of columns visibility management in a simple Silverlight application. The initial application represents the Customers report. The DataSet data source contains the data for the report. The dataSet1 table contains 4 records: Company, Address, Contact, and Phone.

The report template created with the use of the Standard Wizard outputs the data with DataBand in 4 columns:

The generated report in the Silverlight application:

Step 1

Add 4 parameters (showCompany, showAddress, showContact, showPhone) to the Document.Parameters collection in the report designer. Each parameter is responsible for visibility of one of 4 data columns. Set the ParameterType property to System.Boolean.

Step 2

Now you should write the script which will manage the columns position in the report. I.e. shift the column to the left depending on the visible columns. To do this, you need to write the script given below in the editor of the Document.GenerateScript property:

Set the binding of the TextBox.Visible property to the corresponding parameter to manage columns visibility. The property should be set for textboxes that output data for column titles. For example, for the Company column: header1_Customers_Company.Visible = (bool)GetParameter("showCompany")

Step 4

Suppose we need to display only company name and phone in the report. In order to pass these parameters to the report, you should add the code given below to MainPage_Loaded method of the MainPage class before the invocation of the report generation function: