绑定报表

当您按照“项目设置”一节中的说明为本教程做准备时,已经在 Web 窗体上放置了 CrystalReportViewer 控件。但是,要查看报表部件,必须使用 CrystalReportPartsViewer 控件。

在本节中,将实例化 Customers 报表并将其绑定到 CrystalReportPartsViewer 控件。然后,您要测试报表是否显示了您在前面步骤中创建的报表部件。

您可用两种方式实例化并绑定报表:

  • 作为嵌入式报表。
  • 作为非嵌入式报表。

从下面的过程中选择一个(不要两者都选)。

  • 如果使用嵌入式报表,请按照下面这个过程将报表实例化为嵌入式报表。
  • 如果使用非嵌入式报表,请按照第二个过程将报表实例化为非嵌入式报表。

继续浏览“过滤报表中的数据”

将 Customers 报表实例化为嵌入式报表并将其绑定到 CrystalReportPartsViewer 控件

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“设计器”。

  3. 删除“CrystalReportViewer”控件。

  4. 从“工具箱”中,打开“Crystal Reports”节点,找到“CrystalReportPartsViewer”控件。

  5. 将“CrystalReportPartsViewer”控件拖到 Web 窗体上。

  6. 从“属性”窗口中,设置“ID”属性:

    • 对于 Visual Basic 网站,将“ID”属性设置为“myCrystalReportPartsViewer”。
    • 对于 C# 网站,将“ID”属性设置为“crystalReportPartsViewer”。
  7. 从“视图”菜单中,单击“代码”以查看此 Web 窗体的代码隐藏类。

  8. 使用变量名 customersReport,为 Customers 报表包装类添加新的类级声明。将其访问修饰符设置为 private。

    Private customersReport As Customers
    
    private Customers customersReport;
    
  9. 在 ConfigureCrystalReports() 方法中,实例化该报表包装类。

    Note注意

    已在 “项目设置” 中创建了 ConfigureCrystalReports() 方法。

    customersReport = New Customers()
    
    customersReport = new Customers();
    
  10. 在报表实例化代码下面的一行中,将 CrystalReportPartsViewer 控件的 ReportSource 属性绑定到实例化的报表类(变量名:customersReport)。

    myCrystalReportPartsViewer.ReportSource = customersReport
    
    crystalReportPartsViewer.ReportSource = customersReport;
    
    Note注意

    CrystalReportPartsViewer 控件实例在代码中是可访问的,因为您将该控件添加到了 Web 或 Windows 窗体。如果 IntelliSense 未能识别 CrystalReportPartsViewer 控件实例,请验证是否已将 CrystalReportPartsViewer 控件作为类级声明添加到此代码隐藏类。

将 Customers 报表实例化为非嵌入式报表并绑定到 CrystalReportPartsViewer 控件

现在即可生成并运行项目。

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“代码”。

  3. 使用变量名 customersReport,为 ReportDocument 报表包装类添加新的类级声明。将其访问修饰符设置为 private。

``` vb
Private customersReport As ReportDocument
```

``` csharp
private ReportDocument customersReport;
```

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>ReportDocument 类是 CrystalDecisions.CrystalReports.Engine 命名空间的成员。已在&quot;Imports&quot; [Visual Basic]中为此命名空间添加了 &quot;using&quot; [C#] 或 <a href="ms227453(v=vs.90).md">“项目设置”</a> 声明。在实例化 ReportDocument 并将报表加载到命名空间时,即通过 SDK 获取了对报表的访问,而不必嵌入报表。</p></td>
</tr>
</tbody>
</table>
  1. 在 ConfigureCrystalReports() 方法(在“项目设置”的某一过程中添加)中,实例化 ReportDocument 类。

    customersReport = New ReportDocument()
    
    customersReport = new ReportDocument();
    
  2. 声明一个字符串变量,将其命名为“reportPath”,然后将一个本地报表的运行时路径赋值给它。对于网站项目和 Windows 项目,确定此路径时会有所不同:

    • 对于网站,要将本地报表文件的名称作为字符串参数传递到 Server.MapPath() 方法中。这样,在运行时本地报表就会映射到硬盘文件目录路径。

      Dim reportPath As String = Server.MapPath("Customers.rpt")
      
      string reportPath = Server.MapPath("Customers.rpt");
      
    • 对于 Windows 项目,要将 Application.StartupPath 属性与一个反斜杠和本地报表文件名称连接起来。这样,报表将映射到与 Windows 可执行文件相同的目录。

      Note注意

      编译时,需要将报表复制到可执行文件所在的目录。

      Dim reportPath As String = Application.StartupPath & "\" & "Customers.rpt"
      
      string reportPath = Application.StartupPath + "\\" + "Customers.rpt";
      
  3. 调用 ReportDocument 实例的 Load() 方法,并将 reportPath 字符串变量传递给该方法。

``` vb
customersReport.Load(reportPath)
```

``` csharp
customersReport.Load(reportPath);
```
  1. 在下一行中加载报表行的下方,将 CrystalReportPartsViewer 的 ReportSource 属性绑定到 ReportDocument 实例。

    myCrystalReportPartsViewer.ReportSource = customersReport
    
    crystalReportPartsViewer.ReportSource = customersReport;
    

测试 Customers 报表的加载过程

无论选择通过 ReportDocument 类实例化嵌入式报表类还是实例化非嵌入式报表类,所用的变量名都是同一个:customersReport。这使得在后面的过程中可以使用一组公共代码。

现在即可生成并运行项目。预计报表加载将失败,因为此时还没有编写用于设置“城市”参数字段值的代码。您将在本教程的后面部分中为“城市”参数字段添加值。

  1. 从“生成”菜单中选择“生成解决方案”。

  2. 如果生成过程中出错,请立即纠正。

  3. 从“调试”菜单中,单击“开始”。

Customers 报表会显示在 Web 浏览器中。
  1. 单击某个国家/地区以显示地区列表。

  2. 单击某个地区以显示“客户名”、“去年销售额”和“城市”字段。

  3. 返回到 Visual Studio,然后单击“停止”从调试模式中退出。