当按照“项目设置”一节中的说明为本教程做准备时,已经在 Web 或 Windows 窗体上放置了 CrystalReportViewer 控件。在前面的步骤中,您已向项目添加了 CustomersBySalesName 报表和选择公式。
在本节中,会将 CustomersBySalesName 报表的文件目录路径绑定到 CrystalReportViewer 控件。然后测试报表是否显示正确并含有按选择公式所过滤出的记录。
把 CustomersBySalesName 报表的文件目录路径绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
单击“视图”菜单中的“代码”,查看此 Web 或 Windows 窗体的代码隐藏类。
找到在“项目设置”中创建的 ConfigureCrystalReports() 方法。
声明一个字符串变量,将其命名为“reportPath”,然后将一个本地报表的运行时路径赋值给它。对于网站项目和 Windows 项目,确定此路径时会有所不同:
对于网站,要将本地报表文件的名称作为字符串参数传递到 Server.MapPath() 方法中。这样,在运行时本地报表就会映射到硬盘文件目录路径。
Dim reportPath As String = Server.MapPath("CustomersBySalesName.rpt")string reportPath = Server.MapPath("CustomersBySalesName.rpt");对于 Windows 项目,要将 Application.StartupPath 属性与一个反斜杠和本地报表文件名称连接起来。这样,报表将映射到与 Windows 可执行文件相同的目录。
注意编译时,需要将报表复制到可执行文件所在的目录。
Dim reportPath As String = Application.StartupPath & "\" & "CustomersBySalesName.rpt"string reportPath = Application.StartupPath + "\\" + "CustomersBySalesName.rpt";
将 NorthwindCustomers 报表的文件目录路径赋给 CrystalReportViewer 控件的 ReportSource 属性。
myCrystalReportViewer.ReportSource = reportPathcrystalReportViewer.ReportSource = reportPath;
测试 CustomersBySalesName 报表的选择公式
从“生成”菜单中,单击“生成解决方案”。
如果生成过程中出错,请立即纠正。
如果要在 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>
- 从“调试”菜单中,单击“开始”。
Crystal 报表显示四条客户记录:Alley Cat Cycles、Ankara Bicycle Company、Arsenault et Maurier 和 Athens Bicycle Co。
- 返回到 Visual Studio,然后单击“停止”从调试模式中退出。