繫結報表
當您依照「專案設定」一節的指示準備此教學課程時,便已經在 Web Form 上放置 CrystalReportViewer 控制項。然而,若要檢視報表組件,您必須使用 CrystalReportPartsViewer 控制項。
在本節中,您將要產生 Customers 報表的執行個體,然後繫結到 CrystalReportPartsViewer 控制項。接著是測試報表是否能正確顯示您在前幾個程序中所建立的報表組件。
產生報表的執行個體和繫結報表有下列兩個方式:
- 以內嵌報表的方式。
- 以非內嵌報表的方式。
從以下步驟程序中擇一 (不過不能兩個都選) 操作。
- 如果您使用的是內嵌報表,請依照下列步驟程序使報表產生成內嵌報表執行個體。
- 如果您使用的是非內嵌式報表,請依照第二個步驟程序使報表產生成非內嵌式報表執行個體。
請繼續進行「篩選報表中的資料」。
若要使 Customers 報表產生成內嵌報表執行個體,並將報表繫結到 CrystalReportPartsViewer 控制項
開啟 Web Form。
從 [檢視] 功能表中,按一下 [設計工具]。
刪除 [CrystalReportViewer] 控制項。
從 [工具箱] 中開啟 [Crystal Reports] 節點,並找出 [CrystalReportPartsViewer] 控制項。
將 [CrystalReportPartsViewer] 控制項拖曳至 Web Form。
從 [屬性] 視窗中,設定 [ID] 屬性:
- 若是 Visual Basic 網站,請將 [ID] 屬性設為「myCrystalReportPartsViewer」。
- 若是 C# 網站,請將 [ID] 屬性設為「crystalReportPartsViewer」。
按一下 [檢視] 功能表中的 [程式碼],檢視此 Web Form 的程式碼後置類別。
使用變數名稱 customersReport,為 Customers 報表包裝函式類別加入一個類別層級宣告。將其存取修飾詞設成私用。
Private customersReport As Customers
private Customers customersReport;
在 ConfigureCrystalReports() 方法內,產生報表包裝函式類別的執行個體。
附註
您在「專案設定」中建立 ConfigureCrystalReports() 方法。
customersReport = New Customers()
customersReport = new Customers();
在產生報表執行個體下面一行,將 CrystalReportPartsViewer 控制項的 ReportSource 屬性繫結到已產生執行個體的報表類別 (變數名稱:customersReport)。
myCrystalReportPartsViewer.ReportSource = customersReport
crystalReportPartsViewer.ReportSource = customersReport;
附註
CrystalReportPartsViewer 控制項執行個體可從程式碼中存取,因為您已將控制項加入 Web 或 Windows Form 中。如果 IntelliSense 無法識別 CrystalReportPartsViewer 控制項執行個體,請驗證是否已將 CrystalReportPartsViewer 控制項加入此程式碼後置類別做為類別層級的宣告。
若要將 Customers 報表產生成非內嵌報表執行個體,並將報表繫結到 CrystalReportPartsViewer 控制項
您現在已經可以建置及執行您的專案。
開啟 Web Form。
從 [檢視] 功能表,按一下 [程式碼]。
使用變數名稱 customersReport,為 ReportDocument 報表包裝函式類別加入一個新的類別層級宣告。將其存取修飾詞設成私用。
``` vb
Private customersReport As ReportDocument
```
``` csharp
private ReportDocument customersReport;
```
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\36bhtx7w.alert_note(zh-tw,VS.90).gif" alt="Note" class="note" />附註</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>ReportDocument 類別是 CrystalDecisions.CrystalReports.Engine 命名空間的成員。您已經把此命名空間的 "Imports" [Visual Basic] 或 "using" [C#] 宣告加入 <a href="ms227453(v=vs.90).md">「專案設定」</a> 中。當您產生 ReportDocument 執行個體,並且將報表載入命名空間時,就可以在無需嵌入報表的情況下,透過 SDK 存取報表。</p></td>
</tr>
</tbody>
</table>
在 ConfigureCrystalReports() 方法 (在「專案設定」中的某個程序中已加入) 內,產生 ReportDocument 類別的執行個體。
customersReport = New ReportDocument()
customersReport = new ReportDocument();
宣告一個名為 reportPath 的字串變數,並指派本機報表的執行階段路徑。此路徑在網站與 Windows 專案中會有所不同:
如果是網站,請將本機報表檔案的名稱當作字串參數,傳遞給 Server.MapPath() 方法。這麼做可以在執行階段,將本機報表對應到硬碟檔案目錄路徑。
Dim reportPath As String = Server.MapPath("Customers.rpt")
string reportPath = Server.MapPath("Customers.rpt");
如果是 Windows 專案,請用反斜線將 Application.StartupPath 屬性和本機報表檔案名稱串接起來。這麼做可以將報表對應到與 Windows 可執行檔相同的目錄。
附註
編譯時,您會將報表複製到可執行檔所在的目錄中。
Dim reportPath As String = Application.StartupPath & "\" & "Customers.rpt"
string reportPath = Application.StartupPath + "\\" + "Customers.rpt";
呼叫 ReportDocument 執行個體的 Load() 方法,並且在此方法中傳入 reportPath 字串變數。
``` vb
customersReport.Load(reportPath)
```
``` csharp
customersReport.Load(reportPath);
```
在下一行的報表載入底下,將 CrystalReportPartsViewer 的 ReportSource 屬性繫結至 ReportDocument 執行個體。
myCrystalReportPartsViewer.ReportSource = customersReport
crystalReportPartsViewer.ReportSource = customersReport;
若要測試 Customers 報表的載入
不論您是選擇產生內嵌報表類別的執行個體,或者透過 ReportDocument 類別產生非內嵌報表,使用的變數名稱都是 customersReport。這讓您能夠在下列程序中使用通用的程式碼。
您現在已經可以建置及執行您的專案。可預期的是此報表載入將會失敗,因為尚未寫入可以設定 City 參數欄位值的程式碼。稍後您在此教學課程中加入 City 參數欄位的值。
從 [建置] 功能表選取 [建置方案]。
如果發生任何建置錯誤,請立即修正。
從 [偵錯] 功能表中,按一下 [啟動]。
Customers 報表會顯示在 Web 瀏覽器中。
按一下某個國家,以顯示其地區清單。
按一下某個地區,以顯示其 [客戶名稱]、[去年業績] 及 [城市] 等欄位。
返回 Visual Studio,再按一下 [停止] 退出偵錯模式。