共用方式為


繫結報表

「專案設定」中,您已將 CrystalReportViewer 控制項放在 Web 或 Windows Form上。在之前的步驟中,您已將 NorthwindCustomers 報表加入專案中。

在本節中,您將學習如何產生 NorthwindCustomers 報表的執行個體,以及將它繫結到 CrystalReportViewer 控制項。然後,測試報表在其參數欄位尚未設定目前值之前,是否能夠正確顯示。

產生報表的執行個體和繫結報表有下列兩個方式:

  • 以內嵌報表的方式。
  • 以非內嵌報表的方式。

從以下步驟程序中擇一 (不過不能兩個都選) 操作。

  • 如果您使用的是內嵌報表,請依照下列步驟程序使報表產生成內嵌報表執行個體。
  • 如果您使用的是非內嵌式報表,請依照第二個步驟程序使報表產生成非內嵌式報表執行個體。

若要使 NorthwindCustomers 報表產生成內嵌報表執行個體,並將報表繫結到 CrystalReportViewer 控制項

  1. 開啟 Web 或 Windows Form。

  2. 從 [檢視] 功能表,按一下 [程式碼]。

  3. 使用變數名稱 northwindCustomersReport,加入 NorthwindCustomers 報表包裝函式類別的新類別層級宣告。將其存取修飾詞設成私用。

    Private northwindCustomersReport As NorthwindCustomers
    
    private NorthwindCustomers northwindCustomersReport;
    
  4. 在 ConfigureCrystalReports() 方法內,產生報表包裝函式類別的執行個體。

    Note附註

    您在「專案設定」中建立 ConfigureCrystalReports() 方法。

    northwindCustomersReport = New NorthwindCustomers()
    
    northwindCustomersReport = new NorthwindCustomers();
    
  5. 在產生報表執行個體下面一行,將 CrystalReportViewer 控制項的 ReportSource 屬性繫結到已產生執行個體的報表類別 (變數名稱:northwindCustomersReport)。

    myCrystalReportViewer.ReportSource = northwindCustomersReport
    
    crystalReportViewer.ReportSource = northwindCustomersReport;
    

您現在已經可以建置及執行您的專案。可預期的是此報表載入將會失敗,因為尚未撰寫可以登入資料庫的程式碼。

若要使 NorthwindCustomers 報表產生成非內嵌報表執行個體,並將報表繫結到 CrystalReportViewer 控制項

  1. 開啟 Web 或 Windows Form。

  2. 從 [檢視] 功能表,按一下 [程式碼]。

  3. 使用變數名稱 northwindCustomersReport,加入 ReportDocument 報表包裝函式類別的新類別層級宣告。將其存取修飾詞設成私用。

``` vb
Private northwindCustomersReport As ReportDocument
```

``` csharp
private ReportDocument northwindCustomersReport;
```

<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 命名空間的成員。您已經把此命名空間的 &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 類別的執行個體。

    northwindCustomersReport = New ReportDocument()
    
    northwindCustomersReport = new ReportDocument();
    
  2. 宣告一個名為 reportPath 的字串變數,並指派本機報表的執行階段路徑。此路徑在網站與 Windows 專案中會有所不同:

    • 如果是網站,請將本機報表檔案的名稱當作字串參數,傳遞給 Server.MapPath() 方法。這麼做可以在執行階段,將本機報表對應到硬碟檔案目錄路徑。

      Dim reportPath As String = Server.MapPath("NorthwindCustomers.rpt")
      
      string reportPath = Server.MapPath("NorthwindCustomers.rpt");
      
    • 如果是 Windows 專案,請用反斜線將 Application.StartupPath 屬性和本機報表檔案名稱串接起來。這麼做可以將報表對應到與 Windows 可執行檔相同的目錄。

      Note附註

      編譯時,您會將報表複製到可執行檔所在的目錄中。

      Dim reportPath As String = Application.StartupPath & "\" & "NorthwindCustomers.rpt"
      
      string reportPath = Application.StartupPath + "\\" + "NorthwindCustomers.rpt";
      
  3. 呼叫 ReportDocument 執行個體的 Load() 方法,同時傳遞 reportPath 字串變數給此方法。

``` vb
northwindCustomersReport.Load(reportPath)
```

``` csharp
northwindCustomersReport.Load(reportPath);
```
  1. 將 CrystalReportViewer 的 ReportSource 屬性繫結到 ReportDocument 執行個體。

    myCrystalReportViewer.ReportSource = northwindCustomersReport
    
    crystalReportViewer.ReportSource = northwindCustomersReport;
    

若要測試 NorthwindCustomers 報表的載入

不論您是選擇產生內嵌報表類別的執行個體,或者透過 ReportDocument 類別產生非內嵌報表,使用的變數名稱都是 northwindCustomersReport。這讓您能夠在下列程序中使用通用的程式碼。

您現在已經可以建置及執行您的專案。可預期的是此報表載入將會失敗,因為尚未撰寫可以登入資料庫的程式碼。

  1. 從 [建置] 功能表中,選取 [建置方案]。

  2. 如果發生任何建置錯誤,請立即修正。

  3. 如果在 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>
  1. 從 [偵錯] 功能表中,按一下 [啟動]。
NorthwindCustomers 報表並未顯示,因為尚未加入資料庫登入程式碼。

<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 版本而有所不同。舉例而言,如果您已安裝了 Crystal Reports 10 或更新版,就會出現表單,要求您提供報表的資料庫登入資訊。這是 Crystal Reports Developer 的新功能。如果您使用的是舊版的 Crystal Reports,就會出現例外狀況。在兩種情況中,您都必須依照下列步驟程序建立功能完整的應用程式。</p></td>
</tr>
</tbody>
</table>
  1. 返回 Visual Studio,再按一下 [停止] 退出偵錯模式。