Windows Form Viewer 的報表繫結選項
除非另有註明,否則以下案例適用於 Visual Basic .NET 和 C# 應用程式以及 Managed C++ 應用程式。
案例 1:本機報表
假設以下條件存在,而且可以在本機電腦上使用 Crystal Report:
- 在本機電腦的資料夾 C:\Reports 中有一份 Crystal Report(My Report.rpt)。
在 Visual Basic 或 C# 應用程式中
- 您已在預設的資料夾中,為以管理員身份登入的使用者建立了一個 Windows 應用程式專案,名稱為 MyProject (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects)。
- 您已在 MyProject 的 Windows Form 中插入一個 Windows Form Viewer,名稱為
CrystalReportViewer1
。
您可以依報表名稱、報表物件或非型別式報表元件,將 My Report.rpt 繫結至 Windows Form Viewer。
在 C++ 應用程式中
- 您已在預設的資料夾中,為以管理員身份登入的使用者建立了一個 Managed C++ 專案,名稱為 MyProject (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects)。
- 您已在 MyProject 的 MyProject.cpp 中插入一個 Windows Form Viewer,名稱為
CrystalReportViewer1
。
您可以依報表名稱或報表物件,將 My Report.rpt 繫結至 Windows Form Viewer。
依報表名稱繫結
視專案的語言而定,您可以透過 [屬性] 視窗或程式碼來指定 ReportSource 屬性:
在 Visual Basic 或 C# 應用程式中
- 請在 Windows Form 的 [設計] 檢視中,選取 Windows Form Viewer。在 [屬性] 視窗中,為 ReportSource 屬性瀏覽及選取 C:\Reports\My Report.rpt。
- 在程式碼編輯器中,將報表名稱設定為 Windows Form Viewer 的 ReportSource 屬性。
在 C++ 應用程式中
在程式碼編輯器中,將報表名稱設定為 Windows Form Viewer 的 ReportSource 屬性。
範例
將報表名稱設定為 Windows Form Viewer 的 ReportSource 屬性。
[Visual Basic]
CrystalReportViewer1.ReportSource = _
"C:\\Reports\\My Report.rpt"
[C#]
crystalReportViewer1.ReportSource =
"C:\\Reports\\My Report.rpt";
[C++]
crystalReportViewer1->ReportSource =
"C:\\Reports\\My Report.rpt";
[VJ#]
crystalReportViewer1.set_ReportSource("C:\\Reports\\My Report.rpt");
依報表物件繫結
您可以建立報表物件,將報表指定給該物件,以及繫結物件與 Windows Form Viewer。您可以透過報表物件來存取 Crystal Reports for Visual Studio .NET 所顯露的整個物件模型。
若要在 Visual Basic 專案中將報表物件繫結至 Windows Form Viewer。
加入參考至 CrystalDecisions.CrystalReports.Engine。
在程式碼編輯器中開啟 Form1.vb。
將以下命名空間加入至 Form1.vb 中:
Imports CrystalDecisions.CrystalReports.Engine
將報表物件建立為
Form1()
中的公用 ReportDocument 變數:Public WithEvents oRpt As ReportDocument
在
New()
方法中,使用 Report Document 物件載入 My Report.rpt,並將物件繫結至 Windows Form Viewer:Public Sub New() MyBase.New() 'Windows Form 設計工具需要此呼叫。 InitializeComponent() '在 InitializeComponent 呼叫之後 '加入任何初始化 oRpt = New ReportDocument() oRpt.Load("C:\\Reports\\My Report.rpt") CrystalReportViewer1.ReportSource = oRpt End Sub
若要在 C# 專案中將報表物件繫結至 Windows Form Viewer。
加入參考至 CrystalDecisions.CrystalReports.Engine。
在程式碼編輯器中開啟 Form1.vb。
將以下命名空間加入至 Form1.cs 中:
using CrystalDecisions.CrystalReports.Engine;
將報表物件建立為
Form1()
中的公用 ReportDocument 變數:public ReportDocument oRpt=null;
在
Form1()
函式中,使用 Report Document 物件載入 My Report.rpt,並將物件繫結至 Windows Form Viewer:Public Form1() { // // 需要 Windows Form 設計工具支援 // InitializeComponent(); // // 在 InitializeComponent 呼叫之後 // 加入任何建構函式程式碼 // oRpt = new ReportDocument(); oRpt.Load("c:\\Reports\\My Report.rpt"); crystalReportViewer1.ReportSource = oRpt; }
若要在 C++ 專案中將報表物件繫結至 Windows Form Viewer。
在方案總管中按兩下 MyProject.cpp,即可在程式碼編輯器中開啟它。
將下列元件加入至 MyProject.cpp:
#using <C:\Program Files\Common Files\Crystal Decisions\1.1\Managed\CrystalDecisions.CrystalReports.Engine.dll> #using <C:\Program Files\Common Files\Crystal Decisions\1.1\Managed\CrystalDecisions.Windows.Forms.dll>
將下列命名空間加入至 MyProject.cpp:
using namespace CrystalDecisions::CrystalReports::Engine; using namespace CrystalDecisions::Windows::Forms;
將報表物件建立為公用的 ReportDocument 變數:
ReportDocument *oRpt = null;
將 Windows Form Viewer 建立為公用 CrystalReportViewer 變數,並將它繫結至表單物件。
Form* form1 = new Form(); CrystalReportViewer crystalReportViewer1 = new crystalReportViewer form1->Controls->Add(crystalReportViewer1);
使用 ReportDocument 物件載入 My Report.rpt,並將物件繫結至 Windows Form Viewer:
oRpt = new ReportDocument(); oRpt->Load("c:\\Reports\\My Report.rpt"); crystalReportViewer1->ReportSource = oRpt;
依非型別式報表元件繫結
假設您已在 Windows Form 中加入非型別式報表元件 reportDocument1,而且已使用 c:\Reports\My Report.rpt 載入元件。
您可以透過程式碼指定 ReportSource 屬性,將非型別式報表元件繫結至 Windows Form Viewer:
[Visual Basic]
CrystalReportViewer1.ReportSource = reportDocument1
[C#]
crystalReportViewer1.ReportSource = reportDocument1;
[C++] 您只能透過程式碼將報表物件加入至 C++ 應用程式中。如需詳細資訊,請參閱上面的<若要在 C++ 專案中將報表物件繫結至 Windows Form Viewer>章節。若要檢視 C# 或 Visual Basic 範例,請按一下畫面左上角的 [語言篩選條件] 按鈕。
[VJ#]
crystalReportViewer1.set_ReportSource(reportDocument1);
案例 2:已加入至專案的本機報表
假設以下條件存在,而且可以在本機電腦上使用 Crystal Report:
在 Visual Basic 或 C# 應用程式中
- 您已在預設的資料夾中,為以管理員身份登入的使用者建立了一個 Windows 應用程式專案,名稱為 MyProject (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects)。
- 您已在 MyProject 的 Windows Form 中插入一個 Windows Form Viewer,名稱為 CrystalReportViewer1。
- 您已經加入報表 (My Report.rpt) 至 MyProject 中。
您可以依報表物件或強型別報表元件,將 My Report.rpt 繫結至 Windows Form Viewer。
在 C++ 應用程式中
- 您已在預設的資料夾中,為以管理員身份登入的使用者建立了一個 Managed C++ 專案,名稱為 MyProject (C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects)。
您必須透過程式碼來加入 Web Form Viewer 和報表。
依報表物件:
在 Visual Basic 或 C# 應用程式中
當您將 My Report.rpt 加入至 MyProject 中時,便會在 My Report.rpt 的原始程式檔中自動產生對應的報表類別 My_Report。您可以透過 Windows Form 的 [程式碼] 檢視,將對應的報表物件指派給 ReportSource 屬性,即可將 My Report.rpt 繫結至 Windows Form Viewer:
在 C++ 應用程式中
C++ 專案不會自動產生報表類別。您可以透過程式碼,將此類別加入至 C++ 應用程式中。
範例
[Visual Basic]
CrystalReportViewer1.ReportSource = new My_Report()
[C#]
crystalReportViewer1.ReportSource = new My_Report();
[C++]
public __gc Class MyWebClass{
public:
CrystalReportViewer* CrystalReportViewer1;
ReportDocument* oRpt;
void BindReport(){
CrystalReportViewer1 = new CrystalReportViewer();
oRpt = new ReportDocument();
oRpt->Load("c:\\Reports\\My Report.rpt");
CrystalReportViewer1->ReportSource = oRpt;
};
};
[VJ#]
crystalReportViewer1.set_ReportSource(new My_Report());
依強型別報表元件繫結
在 Visual Basic 或 C# 應用程式中
假設您已將 My Report.rpt 當成強型別元件加入至 MyProject 中。
您可以透過 Windows Form 的 [屬性] 視窗或 [程式碼] 檢視指定 ReportSource 屬性,將報表當成元件繫結至 Windows Form Viewer:
- 請在 Windows Form 的 [設計] 檢視中,選取 Windows Form Viewer。在 [屬性] 視窗中,為 ReportSource 屬性從下拉式清單中選取 my_Report1 [MyProject.My_Report]。
- 或者,在 Windows Form 的 [程式碼] 檢視中,設定 Windows Form Viewer 的 ReportSource 屬性。
在 C++ 應用程式中
C++ 專案不會自動產生報表類別。您可以透過程式碼,將此類別加入至 C++ 應用程式中。
範例
[Visual Basic]
CrystalReportViewer1.ReportSource = my_Report1
[C#]
crystalReportViewer1.ReportSource = my_Report1;
[C++]
public __gc Class MyWebClass{
public:
CrystalReportViewer* CrystalReportViewer1;
ReportDocument* oRpt;
void BindReport(){
CrystalReportViewer1 = new CrystalReportViewer();
oRpt = new ReportDocument();
oRpt->Load("c:\\Reports\\My Report.rpt");
CrystalReportViewer1->ReportSource = oRpt;
};
};
[VJ#]
crystalReportViewer1.set_ReportSource( my_Report1);