绑定报表
当按照“项目设置”一节中的说明为本教程做准备时,已经在 Web 或 Windows 窗体上放置了 CrystalReportViewer 控件。在前面的步骤中,已经为项目添加了一个 CustomersByCity 报表。
在本节中,首先将实例化 CustomersByCity 报表,并将其绑定到 CrystalReportViewer 控件。然后,测试在没有为报表的参数字段设置当前值的情况下,报表是否正确显示。
您可用两种方式实例化并绑定报表:
- 作为嵌入式报表。
- 作为非嵌入式报表。
从下面的过程中选择一个(不要两者都选)。
- 如果使用嵌入式报表,请按照下面这个过程将报表实例化为嵌入式报表。
- 如果使用非嵌入式报表,请按照第二个过程将报表实例化为非嵌入式报表。
将 CustomersByCity 报表实例化为嵌入式报表并绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
单击“视图”菜单中的“代码”,查看此 Web 或 Windows 窗体的代码隐藏类。
使用变量名 customersByCityReport 为 CustomersByCity 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
Private customersByCityReport As CustomersByCity
private CustomersByCity customersByCityReport;
在 ConfigureCrystalReports() 方法中,实例化该报表包装类。
<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>已在 <a href="ms227453(v=vs.90).md">“项目设置”</a> 中创建了 ConfigureCrystalReports() 方法。</p></td>
</tr>
</tbody>
</table>
``` vb
customersByCityReport = New CustomersByCity()
```
``` csharp
customersByCityReport = new CustomersByCity();
```
在报表实例化代码下面的一行中,将 CrystalReportViewer 控件的 ReportSource 属性绑定到实例化的报表类(变量名:customersByCityReport)。
myCrystalReportViewer.ReportSource = customersByCityReport
crystalReportViewer.ReportSource = customersByCityReport;
注意 CrystalReportViewer 控件实例在代码中是可访问的,因为之前已将该控件添加到 Web 或 Windows 窗体中。如果 Intellisense 未能识别 CrystalReportViewer 控件实例,请验证是否已将 CrystalReportViewer 控件作为类级声明添加到此代码隐藏类。
将 CustomersByCity 报表实例化为非嵌入式报表并绑定到 CrystalReportViewer 控件
现在即可生成并运行项目。预计报表加载将失败,因为此时还没有编写用于设置“城市”参数字段值的代码。
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
使用变量名 customersByCityReport 为 ReportDocument 报表包装类添加新的类级声明。将其访问修饰符设置为 private。
Private customersByCityReport As ReportDocument
private ReportDocument customersByCityReport;
注意 ReportDocument 类是 CrystalDecisions.CrystalReports.Engine 命名空间的成员。已在"Imports" [Visual Basic]中为此命名空间添加了 "using" [C#] 或 “项目设置” 声明。在实例化 ReportDocument 并将报表加载到命名空间时,即通过 SDK 获取了对报表的访问,而不必嵌入报表。
在 ConfigureCrystalReports() 方法(在“项目设置”的某一过程中添加)中,实例化 ReportDocument 类。
``` vb
customersByCityReport = New ReportDocument()
```
``` csharp
customersByCityReport = new ReportDocument();
```
声明一个字符串变量,将其命名为“reportPath”,然后将一个本地报表的运行时路径赋值给它。对于网站项目和 Windows 项目,确定此路径时会有所不同:
对于网站,要将本地报表文件的名称作为字符串参数传递到 Server.MapPath() 方法中。这样,在运行时本地报表就会映射到硬盘文件目录路径。
Dim reportPath As String = Server.MapPath("CustomersByCity.rpt")
string reportPath = Server.MapPath("CustomersByCity.rpt");
对于 Windows 项目,要将 Application.StartupPath 属性与一个反斜杠和本地报表文件名称连接起来。这样,报表将映射到与 Windows 可执行文件相同的目录。
注意 编译时,需要将报表复制到可执行文件所在的目录。
Dim reportPath As String = Application.StartupPath & "\" & "CustomersByCity.rpt"
string reportPath = Application.StartupPath + "\\" + "CustomersByCity.rpt";
调用 ReportDocument 实例的 Load() 方法,并将 reportPath 字符串变量传递给该方法。
``` vb
customersByCityReport.Load(reportPath)
```
``` csharp
customersByCityReport.Load(reportPath);
```
在报表加载部分的下一行中,将 CrystalReportViewer 的 ReportSource 属性绑定到 ReportDocument 实例。
myCrystalReportViewer.ReportSource = customersByCityReport
crystalReportViewer.ReportSource = customersByCityReport;
无论选择通过 ReportDocument 类实例化嵌入式报表类还是实例化非嵌入式报表类,所用的变量名都是同一个:customersByCityReport。这使得在后面的过程中可以使用一组公共代码。
现在即可生成并运行项目。预计报表加载将失败,因为此时还没有编写用于设置“城市”参数字段值的代码。您将在本教程的后面部分中为“城市”参数字段添加值。
测试 CustomersByCity 报表的加载过程
从“生成”菜单中选择“生成解决方案”。
如果生成过程中出错,请立即纠正。
如果要在 Windows 项目中使用非嵌入式报表,请在 \bin\ [Visual Basic] 子目录或 \bin\debug\ [C#] 子目录中找到编译后的 Windows 可执行文件,然后将报表复制到该子目录。
<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>要让 Windows 可执行文件在运行时加载非嵌入式报表,该报表必须与 Windows 可执行文件存储在同一个目录中。</p></td>
</tr>
</tbody>
</table>
- 从“调试”菜单中,单击“开始”。
CustomersByCity 报表并不显示。它会在本教程后面部分中为“城市”参数字段添加值以后才显示。
<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>结果可能会因为所使用的 Crystal Reports 版本不同而不同。在较新的版本中,您会看见一个窗体,要求您为该报表提供参数值。在较早的版本中,则会引发“丢失参数字段当前值”异常。无论哪一种情况,都必须添加其他代码才能创建功能完整的应用程序。</p></td>
</tr>
</tbody>
</table>
- 返回到 Visual Studio,然后单击“停止”从调试模式中退出。