繫結報表
當您依照「專案設定」一節的指示準備此教學課程時,便已完成在 Web 或 Windows Form 中放置 CrystalReportViewer 控制項的動作。於之前的步驟中,您已經將 CustomersByCity 報表加入專案中。
在本節中,您將學習如何產生 CustomersByCity 報表的執行個體,並將它繫結到 CrystalReportViewer 控制項。然後,測試報表在其參數欄位尚未設定目前值之前,是否能夠正確顯示。
產生報表的執行個體和繫結報表有下列兩個方式:
- 以內嵌報表的方式。
- 以非內嵌報表的方式。
從以下步驟程序中擇一 (不過不能兩個都選) 操作。
- 如果您使用的是內嵌報表,請依照下列步驟程序使報表產生成內嵌報表執行個體。
- 如果您使用的是非內嵌式報表,請依照第二個步驟程序使報表產生成非內嵌式報表執行個體。
若要使 CustomersByCity 產生成內嵌報表執行個體,並將報表繫結到 CrystalReportViewer 控制項
開啟 Web 或 Windows Form。
按一下 [檢視] 功能表中的 [程式碼],檢視此 Web Form 或 Windows Form 的程式碼後置類別。
使用變數名稱 customersByCityReport,加入 CustomersByCity 報表包裝函式類別的新類別層級宣告。將其存取修飾詞設成私用。
Private customersByCityReport As CustomersByCity
private CustomersByCity customersByCityReport;
在 ConfigureCrystalReports() 方法內,產生報表包裝函式類別的執行個體。
<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>您在<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 Form 中。如果 Intellisense 無法識別 CrystalReportViewer 控制項執行個體,請驗證是否已將 CrystalReportViewer 控制項加入此程式碼後置類別做為類別層級的宣告。
若要使 CustomersByCity 報表產生成非內嵌報表執行個體,並將報表繫結到 CrystalReportViewer 控制項
您現在已經可以建置及執行您的專案。可預期的是此報表載入將會失敗,因為尚未寫入可以設定 City 參數欄位值的程式碼。
開啟 Web 或 Windows Form。
從 [檢視] 功能表,按一下 [程式碼]。
使用變數名稱 customersByCityReport,新增 ReportDocument 報表包裝函式類別的類別層級宣告。將其存取修飾詞設成私用。
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。這讓您能夠在下列程序中使用通用的程式碼。
您現在已經可以建置及執行您的專案。可預期的是此報表載入將會失敗,因為尚未寫入可以設定 City 參數欄位值的程式碼。稍後您在此教學課程中加入 City 參數欄位的值。
若要測試 CustomersByCity 報表的載入
從 [建置] 功能表選取 [建置方案]。
如果發生任何建置錯誤,請立即修正。
如果在 Windows 專案中使用非內嵌報表,請在 \bin\ [Visual Basic] 或 \bin\debug\ [C#] 子目錄中找出已編譯的 Windows 可執行檔,然後將報表複製到該子目錄中。
<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>如果希望 Windows 可執行檔在執行階段載入非內嵌報表,則此報表應和 Windows 可執行檔儲存在同一個目錄中。</p></td>
</tr>
</tbody>
</table>
- 從 [偵錯] 功能表中,按一下 [啟動]。
CustomersByCity 報表不會顯示。稍後您在此教學課程中加入 City 參數欄位的值後,此報表才會顯示。
<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>結果可能會因您使用的 Crystal Reports 版本而有所不同。在較新的版本中,您可以看到要求您提供該報表之參數值的表單。在較舊的版本中,則會出現「遺失參數欄位目前值」例外狀況。不管如何,您都必須再加入更多的程式碼才能建立功能完整的應用程式。</p></td>
</tr>
</tbody>
</table>
- 返回 Visual Studio,再按一下 [停止] 退出偵錯模式。