Using Parameters and Scripts for Managing Columns Visibility

Download the sample application: SelectColumns.zip

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

  1. To declare the parameters in the template (the Document.Parameters property collection). 
  2. 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. 
  3. 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:

PerpetuumSoft.Framework.Drawing.Vector leftColumn = 
new PerpetuumSoft.Framework.Drawing.Vector(1.5, 0).
ConvertUnits(Unit.Centimeter, Unit.InternalUnit);
if ((bool) GetParameter("showCompany"))
leftColumn.X += header1_Customers_Company.Size.Width;
header1_Customers_Address.Location = leftColumn; 
detail1_Customers_Address.Location = leftColumn;
if ((bool) GetParameter("showAddress"))
leftColumn.X += header1_Customers_Address.Size.Width;
header1_Customers_Contact.Location = leftColumn; 
detail1_Customers_Contact.Location = leftColumn;
if ((bool) GetParameter("showContact"))
leftColumn.X += header1_Customers_Contact.Size.Width;
header1_Customers_Phone.Location = leftColumn; 
detail1_Customers_Phone.Location = leftColumn;

Step 3

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:

reportViewer.Parameters.Add("showCompany", true);
reportViewer.Parameters.Add("showAddress", false);
reportViewer.Parameters.Add("showContact", false);
reportViewer.Parameters.Add("showPhone", true);
reportViewer.ApplyTemplate();

The generated report in the Silverlight application:

Add Feedback