Share via


Binding to an Embedded Report Class Upcast to ReportDocument

Object Model

This report binding scenario uses Report Binding with ReportDocument Object Model.

Location of Reports

Reports are embedded into the project.

Note

Previous versions of this documentation referred to embedded reports as "strongly-typed." The term now used to refer to a report that is added or imported into a Visual Studio project is "embedded."

Description

This report binding scenario extends from the scenario Binding to an Embedded Report Class. In this scenario, you bind many embedded reports to a project that uses one common set of code, rather than having to code each one individually.

In this report binding scenario, you add multiple embedded reports to the project. But you do not bind any of the embedded reports to the CrystalReportViewer control directly. Instead, you run a utility method that uses selection criteria to select one of the embedded reports. The utility upcasts the report into an instance of ReportDocument. The ReportDocument instance is then bound to the control.

Note

ReportDocument is a base class that all embedded reports inherit from. Therefore, any embedded reports can upcast to ReportDocument.

Pros

  • Improved code through refactoring: a common set of report binding code interacts with any one of multiple embedded reports. With the previous scenario you would increase your workload by having to manually code each embedded report.
  • Less coding through code sharing: you can write additional code against the instance of ReportDocument, to fully interact with the ReportDocument object model. All of that code is written one time only, but acts upon each succeeding embedded report that is upcast into the ReportDocument instance.
  • All other pros in Binding to an Embedded Report Class.

Cons

  • Added maintenance: changes to reports require recompiling and redistributing the application.

To bind to an embedded report class that is upcast to ReportDocument

Note

This procedure works only with a project that has been created from Project Setup. Project Setup contains specific namespace references and code configuration that is required for this procedure, and you will be unable to complete the procedure without that configuration. Therefore, before you begin this procedure, you must first follow the steps in Project Setup.

  1. Add two additional sample reports to the project:

  2. Add an "Imports" [Visual Basic] or "using" [C#] statement to the top of the class for the CrystalDecisions.CrystalReports.Engine namespace.

    Note

    You must declare this namespace to use the ReportDocument class without a namespace prefix, and to access it from IntelliSense.

    Imports CrystalDecisions.CrystalReports.Engine
    
    using CrystalDecisions.CrystalReports.Engine;
    
  3. Create a private helper method for report selection, as shown below.

    This method receives an integer parameter and passes it through a "Select Case" [Visual Basic] or "switch" [C#] statement to select a report. In each case, it instantiates and returns a particular report (upcast to its base class, ReportDocument).

    Private Function ChooseReport(ByVal i As Integer) As ReportDocument
        Select Case i
            Case 1
                Dim chartReport As Chart = New Chart()
                Return CType(chartReport, ReportDocument)
            Case 2
                Dim hierarchicalGroupingReport As Hierarchical_Grouping
    = New Hierarchical_Grouping()
                Return CType(hierarchicalGroupingReport,
    ReportDocument)
            Case Else
                Dim worldSalesReport As World_Sales_Report = New
    World_Sales_Report()
                Return CType(worldSalesReport, ReportDocument)
        End Select
    End Function
    
    private ReportDocument ChooseReport(int i)
    {
       switch(i)
       {
          case 1:
             Chart chartReport = new Chart();
             return (ReportDocument)chartReport;
          case 2:
             Hierarchical_Grouping hierarchicalGroupingReport = new
    Hierarchical_Grouping();
             return (ReportDocument)hierarchicalGroupingReport;
          default:
             World_Sales_Report worldSalesReport = new
    World_Sales_Report ();
             return (ReportDocument)worldSalesReport;                  
    
       };
    }
    
  4. Within the ConfigureCrystalReports() method (that you have created in Project Setup), delete any existing code and replace it with the following new lines of code.

  5. Declare an instance of ReportDocument and populate it by calling the ChooseReport() method, passing in the integer 1 to select the first report, Chart.rpt.

    Dim myReportDocument As ReportDocument = ChooseReport(1)
    
    ReportDocument reportDocument = ChooseReport(1);
    
  6. You can create a placeholder in which to code against the ReportDocument instance.

In this example, report data could be filtered with the RecordSelectionFormula property. Because ReportDocument represents a variety of possible reports, the filter is applied to any report that is passed into it. For now, pass in an empty string.


> [!NOTE]
> <P>This line of code demonstrates how an upcast to the common base class, ReportDocument, allows you to write common code against multiple reports.</P>


``` vb
myReportDocument.RecordSelectionFormula = ""
```

``` csharp
reportDocument.RecordSelectionFormula = "";
```
  1. Assign the ReportDocument instance to the ReportSource property of the CrystalReportViewer control.

    myCrystalReportViewer.ReportSource = myReportDocument
    
    crystalReportViewer.ReportSource = reportDocument;
    
  2. To view the report, build and run your project.

See Also