Export (0) Print
Expand All

Report Binding Options for Windows Forms Viewers

Visual Studio .NET 2003

The following scenarios apply to Visual Basic .NET and C# applications, as well as Managed C++ applications unless otherwise stated.

Scenario 1: Local Report

Suppose the following conditions exist and a Crystal report is available on a local machine:

  • You have a Crystal report, My Report.rpt, in the folder, C:\Reports, on a local machine.

In a Visual Basic or C# application

  • You have created a Windows Application project, MyProject, in the default folder for the user logged on as Administrator (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects).
  • You have inserted a Windows Forms Viewer, CrystalReportViewer1, in the Windows Form, in MyProject.

You can bind My Report.rpt to the Windows Forms Viewer by report name, report object or untyped report component.

In a C++ application

  • You have created a Managed C++ project, MyProject, in the default folder for the user logged on as Administrator (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects).
  • You have inserted a Windows Forms Viewer, crystalReportViewer1, in MyProject.cpp in MyProject.

You can bind My Report.rpt to the Windows Forms Viewer by report name, or report object.

Binding by Report Name

Depending on the language of your project, you can specify the ReportSource property through the Properties window or code:

In a Visual Basic or C# application

  • Select the Windows Forms Viewer in the Design view of the Windows Form. In the Properties window, browse to select "C:\Reports\My Report.rpt" for the ReportSource property.
  • In the Code Editor.set the report name to the Windows Forms Viewer's ReportSource property.

In a C++ application

In the Code Editor.set the report name to the Windows Forms Viewer's ReportSource property.

Example

Set the report name to the Windows Forms Viewer's ReportSource property.

[Visual Basic]
CrystalReportViewer1.ReportSource = _
"C:\\Reports\\My Report.rpt"
[C#]
crystalReportViewer1.ReportSource =
"C:\\Reports\\My Report.rpt";
[C++]
crystalReportViewer1->ReportSource =
"C:\\Reports\\My Report.rpt";
[VJ#]
crystalReportViewer1.set_ReportSource(
"C:\\Reports\\My Report.rpt");

Binding by Report Object

You may create a report object, assign a report to that object, and bind the object to a Windows Forms Viewer. Through the report object, you can access the entire object model exposed by Crystal Reports for Visual Studio .NET.

To bind a report object to the Windows Forms Viewer in a Visual Basic project.

  1. Add a reference to CrystalDecisions.CrystalReports.Engine.
  2. Open Form1.vb in the Code Editor.
  3. Add the following namespace to Form1.vb:
    Imports CrystalDecisions.CrystalReports.Engine
    
  4. Create a report object as a public ReportDocument variable in Form1():
    Public WithEvents oRpt As ReportDocument
    
  5. In the method New(), use the Report Document object to load My Report.rpt, and bind the object to the Windows Forms Viewer:
    Public Sub New()
       MyBase.New()
    
       'This call is required by the Windows Form Designer.
       InitializeComponent()
    
       'Add any initialization after the 
       'InitializeComponent() call
       oRpt = New ReportDocument()
       oRpt.Load("C:\\Reports\\My Report.rpt")
       CrystalReportViewer1.ReportSource = oRpt
    End Sub
    

To bind a report object to the Windows Forms Viewer in a C# project.

  1. Add a reference to CrystalDecisions.CrystalReports.Engine.
  2. Open Form1.cs in the Code Editor.
  3. Add the following namespace to Form1.cs:
    using CrystalDecisions.CrystalReports.Engine;
    
  4. Create a report object as a public ReportDocument variable in Form1():
    public ReportDocument oRpt=null;
    
  5. In the function Form1(), use the Report Document object to load My Report.rpt, and bind the object to the Windows Forms Viewer:
    Public Form1()
    {
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();
    
       //
       // TODO: Add any constructor code after InitializeComponent 
       // call
       //
       oRpt = new ReportDocument();
      oRpt.Load("c:\\Reports\\My Report.rpt");   
       crystalReportViewer1.ReportSource = oRpt;
    }
    

To bind a report object to the Windows Forms Viewer in a C++ project.

  1. Double-click MyProject.cpp in the Solution Explorer to open it in the Code Editor.
  2. Add the following components to MyProject.cpp:
    #using <C:\Program Files\Common Files\Crystal Decisions\1.1\Managed\CrystalDecisions.CrystalReports.Engine.dll>
    #using <C:\Program Files\Common Files\Crystal Decisions\1.1\Managed\CrystalDecisions.Windows.Forms.dll>
    
  3. Add the following namespaces to MyProject.cpp:
    using namespace CrystalDecisions::CrystalReports::Engine;
    using namespace CrystalDecisions::Windows::Forms;
    
  4. Create a report object as a public ReportDocument variable:
    ReportDocument *oRpt = null;
    
  5. Create a Windows Forms Viewer as a public CrystalReportViewer variable and bind it to a form object.
    Form* form1 = new Form();
    CrystalReportViewer crystalReportViewer1 = new crystalReportViewer
    form1->Controls->Add(crystalReportViewer1);
    
  6. Use the Report Document object to load My Report.rpt, and bind the object to the Windows Forms Viewer:
    oRpt = new ReportDocument();
    oRpt->Load("c:\\Reports\\My Report.rpt");
    crystalReportViewer1->ReportSource = oRpt;
    

Binding by Untyped Report Component

Suppose you have added an untyped report component, reportDocument1, to the Windows Form, and have loaded the component with c:\Reports\My Report.rpt.

You can bind the untyped report component to the Windows Forms Viewer by specifying the ReportSource property through code:

[Visual Basic]
CrystalReportViewer1.ReportSource = reportDocument1
[C#]
crystalReportViewer1.ReportSource = reportDocument1;

[C++] You can add report objects to your C++ application only through code. For more information see To bind a report object to the Windows Forms Viewer in a C++ project above. To view a C# or VB example, click the Language Filter button in the upper-left corner of the page.

[VJ#]
crystalReportViewer1.set_ReportSource(reportDocument1);

Scenario 2: Local Report Added to Project

Suppose the following conditions exist and a Crystal report is available on a local machine:

In a Visual Basic or C# application

  • You have created a Windows Application project, MyProject, in the default folder for the user logged on as Administrator (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects).
  • You have inserted a Windows Forms Viewer, CrystalReportViewer1 in the Windows Form, in MyProject.
  • You have added the report My Report.rpt to MyProject.

You can bind My Report.rpt to the Windows Forms Viewer by report object or strongly-typed report component.

In a C++ application

  • You have created a Managed C++ project, MyProject, in the default folder for the user logged on as Administrator (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects).

You must add your Web Forms Viewer and report through code

By report object:

In a Visual Basic or C# application

When you add My Report.rpt to MyProject, the corresponding report class, My_Report, is automatically generated in the source file for My Report.rpt. You can bind My Report.rpt to the Windows Forms Viewer by assigning the corresponding report object to the ReportSource property through the Code view for the Windows Form:

In a C++ application

No report class is automatically generated for C++ projects. You can add this class to your C++ application through code.

Example

[Visual Basic]
CrystalReportViewer1.ReportSource = new My_Report()
[C#]
crystalReportViewer1.ReportSource = new My_Report();
[C++]
public __gc Class MyWebClass{
public:
   CrystalReportViewer* CrystalReportViewer1;
   ReportDocument* oRpt;
   void BindReport(){
      CrystalReportViewer1 = new CrystalReportViewer();
      oRpt = new ReportDocument();
      oRpt->Load("c:\\Reports\\My Report.rpt");   
      CrystalReportViewer1->ReportSource = oRpt;
   };
};
[VJ#]
crystalReportViewer1.set_ReportSource(new My_Report());

Binding by Strongly-Typed Report Component

In a Visual Basic or C# application

Suppose you have added My Report.rpt as a strongly-typed component to MyProject.

You can bind the report as a component to the Windows Forms Viewer by specifying the ReportSource property through either the Properties window or the Code view for the Windows Form:

  • Select the Windows Forms Viewer in the Design view of the Windows Form. In the Properties window, select my_Report1 [MyProject.My_Report] from the drop down list for the ReportSource property.
  • Or, in the Code view of the Windows Form, set the ReportSource property of the Windows Forms Viewer.

In a C++ application

No report class is automatically generated for C++ projects. You can add this class to your C++ application through code.

Example

[Visual Basic]
CrystalReportViewer1.ReportSource = my_Report1
[C#]
crystalReportViewer1.ReportSource = my_Report1;
[C++]
public __gc Class MyWebClass{
public:
   CrystalReportViewer* CrystalReportViewer1;
   ReportDocument* oRpt;
   void BindReport(){
      CrystalReportViewer1 = new CrystalReportViewer();
      oRpt = new ReportDocument();
      oRpt->Load("c:\\Reports\\My Report.rpt");   
      CrystalReportViewer1->ReportSource = oRpt;
   };
};
[VJ#]
crystalReportViewer1.set_ReportSource( my_Report1);
Show:
© 2014 Microsoft