この例では、実行時にファイルからレポート定義を読み込みます。このようなアプリケーションは、レポート定義ファイルを必要とし、ビューアのように機能します。このようなアプリケーションの主な長所は、レポート形式を変更しても、アプリケーションを更新する必要がないことです。新しいレポート定義ファイルをユーザーに送るだけで済みます。
実行時に読み込まれるレポートを備えたアプリケーションを作成するには、次の手順に従います。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Imports C1.C1Report Imports System.IO |
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
using C1.C1Report; using System.IO; |
|
これにより、完全な名前空間を指定しなくても、C1Report と System.IO のクラスとオブジェクトを参照できます。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
' アプリケーションパスを取得します。
Dim appPath As String
appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower()
Dim i As Integer = appPath.IndexOf("/bin")
If (i < 0) Then i = appPath.IndexOf("\bin")
If (i > 0) Then appPath = appPath.Remove(i, appPath.Length - i)
' レポート定義ファイル内の全レポートのリストを取得します。
m_ReportDefinitionFile = appPath & "\Data\Nwind.xml"
Dim reports As String() = c1r.GetReportInfo(m_ReportDefinitionFile)
' コンボボックスに代入します。
cmbReport.Items.Clear()
Dim report As String
For Each report In reports
cmbReport.Items.Add(report)
Next
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
// アプリケーションパスを取得します。
string appPath;
appPath = Path.GetDirectoryName(Application.ExecutablePath).ToLower();
int i = appPath.IndexOf("/bin");
if ((i < 0) ) { i = appPath.IndexOf("\bin"); }
if ((i > 0) ) { appPath = appPath.Remove(i, appPath.Length - i); }
// レポート定義ファイル内の全レポートのリストを取得します。
m_ReportDefinitionFile = appPath + "\Data\Nwind.xml";
string ( reports) = c1r.GetReportInfo(m_ReportDefinitionFile);
// コンボボックスに代入します。
cmbReport.Items.Clear();
string report;
foreach report In reports
cmbReport.Items.Add(report);
}
|
|
上のコードは、最初にレポート定義を含むファイルの場所を取得しています。そのために、システム定義の Path クラスおよび Application クラスの静的メソッドを使用します。このコードは、実際のレポート定義ファイルの場所と名前に合わせて変更する必要があります。
次に、GetReportInfo メソッドを使用してレポート定義ファイル(手順1で作成)内のすべてのレポート名を含む配列を取得し、ユーザーがレポートを選択するためのコンボボックスにレポート名を挿入します。
Visual Basic コードの書き方
| Visual Basic |
コードのコピー
|
|---|---|
Private Sub cmbReport_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbReport.SelectedIndexChanged
Try
Cursor = Cursors.WaitCursor
' レポートを読み込みます。
status.Text = "Loading " & cmbReport.Text
c1r.Load(m_ReportDefinitionFile, cmbReport.Text)
' PrintPreview コントロール内に出力します。
status.Text = "Rendering " & cmbReport.Text
ppv.Document = c1r.Document
' PrintPreview コントロールにフォーカスを移します。
ppv.StartPage = 0
ppv.Focus()
Finally
Cursor = Cursors.Default
End Try
End Sub
|
|
C# コードの書き方
| C# |
コードのコピー
|
|---|---|
private void cmbReport_SelectedIndexChanged(object sender, System.EventArgs e)
{
try {
Cursor = Cursors.WaitCursor;
// レポートを読み込みます。
status.Text = "Loading " + cmbReport.Text;
c1r.Load(m_ReportDefinitionFile, cmbReport.Text);
// PrintPreview コントロール内に出力します。
status.Text = "Rendering " + cmbReport.Text;
ppv.Document = c1r.Document;
// PrintPreview コントロールにフォーカスを移します。
ppv.StartPage = 0;
ppv.Focus();
} finally {
Cursor = Cursors.Default;
}
}
|
|