作成日: 2020/04/15 最終更新日: 2021/04/12
文書種別
使用方法
詳細
セクションレポートの場合
セクションレポートでは以下のような方法が考えられます。
1. レポート外のイベントからレポート内のコントロールを操作する方法
SectionsおよびControlsクラスを使用し、オブジェクトを適切な型にキャストすることで、レポート外のイベントからレポート内のコントロールに直接アクセスすることが可能です。
◆サンプルコード(C#)
var rpt = new SectionReport1();
// ページヘッダ上の「TextBox1」に値を設定します。
// ※この時、必ず適切な型にキャストする必要があります。
((GrapeCity.ActiveReports.SectionReportModel.TextBox)(rpt.Sections["PageHeader"].Controls["TextBox1"])).Text = "Sample Text";
// レポートを実行します。
rpt.Run();
◆サンプルコード(VB.NET)
Dim rpt As New SectionReport1()
' ページヘッダ上の「TextBox1」に値を設定します。
' ※この時、必ず適切な型にキャストする必要があります。
DirectCast(rpt.Sections("PageHeader").Controls("TextBox1"), GrapeCity.ActiveReports.SectionReportModel.TextBox).Text = "Sample Text"
' レポートを実行します。
rpt.Run()
2. レポート内で定義したPublic変数に値を渡す方法
レポート内でPublic変数を定義し、生成したレポートインスタンスに値をセットして渡す方法です。
◆サンプルコード(C#)
【レポート内のコード】
public string strParam;
private void SectionReport1_ReportStart(...)
{
this.TextBox1.Text = strParam;
}
【レポートを呼び出す側のコード】
SectionReport1 rpt = new SectionReport1();
rpt.strParam = "Sample Text";
rpt.Run();
◆サンプルコード(VB.NET)
【レポート内のコード】
Public strParam As String
Private Sub SectionReport1_ReportStart(...) Handles MyBase.ReportStart
Me.TextBox1.Text = strParam
End Sub
【レポートを呼び出す側のコード】
Dim rpt As SectionReport1 = New SectionReport1()
rpt.strParam = "Sample Text"
rpt.Run()
3. レポート内でコンストラクタを定義して値を渡す方法
レポート内でコンストラクタを定義し、レポートインスタンス生成時に値を渡す方法です。
◆サンプルコード(C#)
【レポート内のコード】
private string myStrInput;
public SectionReport1(string StrInput)
{
// デザイナー サポートに必要なメソッドです。
InitializeComponent();
// 引数として渡された値を変数に格納します。
myStrInput = StrInput;
}
private void SectionReport1_ReportStart(...)
{
this.TextBox1.Text = myStrInput;
}
【レポートを呼び出す側のコード】
SectionReport1 rpt = new SectionReport1("Sample Text");
rpt.Run();
◆サンプルコード(VB.NET)
【レポート内のコード】
Private myStrInput As String
Public Sub New(ByVal StrInput As String)
' この呼び出しはデザイナーで必要です。
InitializeComponent()
' 引数として渡された値を変数に格納します。
myStrInput = StrInput
End Sub
Private Sub SectionReport1_ReportStart(...) Handles MyBase.ReportStart
Me.TextBox1.Text = myStrInput
End Sub
【レポートを呼び出す側のコード】
Dim rpt As SectionReport1 = New SectionReport1("Sample Text")
rpt.Run()
4. パラメータを使用する方法
レポート内にパラメータを定義し、それを経由して値を渡す方法です。
具体的には、以下のような手順になります。
(1) ソリューションエクスプローラからパラメータを追加します。
◆パラメータの定義例
プロパティ | 設定例 |
Name | Parameter1 |
PromptUser | False(※) |
Type | String |
※PromptUserプロパティ(パラメータ入力ダイアログの表示有無)は、必ず"False"に設定してください。
パラメータの使用方法や詳細については、製品ヘルプの以下のトピックをご覧ください。
(2) TextBoxやLabelコントロールのDataFieldプロパティを以下のように設定することで、パラメータに設定された値がそのコントロールに表示されます。
=param:Parameter1
また、以下のようにコード内でパラメータを参照し、コントロールに設定することも可能です。
◆サンプルコード(C#)
this.TextBox1.Text = this.Parameters["Parameter1"].Value;
◆サンプルコード(VB.NET)
Me.TextBox1.Text = Me.Parameters("Parameter1").Value
(3) レポートを呼び出すコード側で、レポートのパラメータに渡したい値を設定します。
◆サンプルコード(C#)
SectionReport1 rpt = new SectionReport1();
rpt.Parameters["Parameter1"].Value = "Sample Text";
rpt.Run();
◆サンプルコード(VB.NET)
Dim rpt As SectionReport1 = New SectionReport1()
rpt.Parameters("Parameter1").Value = "Sample Text"
rpt.Run()
ページレポート/RDLレポートの場合
パラメータを使用することでレポート外部のコードからレポートに値を渡すことが可能です。
具体的には、以下のような手順になります。
(1) ソリューションエクスプローラからパラメータを追加します。
◆パラメータの定義例
設定項目 | 設定例 |
名前 | ReportParameter1 |
データタイプ | String |
非表示 | オン(※) |
※「非表示」チェックボックスは、必ずオンに設定してください。
パラメータの使用方法や詳細については、製品ヘルプの以下のトピックをご覧ください。
(2) TextBoxコントロールのValueプロパティに以下のような式を設定することで、パラメータに設定された値がそのコントロールに表示されます。
=Parameters!ReportParameter1.Value
(3) レポートを呼び出すコード側で、レポートのパラメータに渡したい値を設定します。
◆サンプルコード(C#)
var pRep = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo("PageReport1.rdlx"));
var pDoc = new GrapeCity.ActiveReports.Document.PageDocument(pRep);
pDoc.Parameters["ReportParameter1"].CurrentValue = "Sample Text";
this.viewer1.LoadDocument(pDoc);
◆サンプルコード(VB.NET)
Dim pRep As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo("PageReport1.rdlx"))
Dim pDoc As New GrapeCity.ActiveReports.Document.PageDocument(pRep)
pDoc.Parameters("ReportParameter1").CurrentValue = "Sample Text"
Me.Viewer.LoadDocument(pDoc)
※WebViewerコントロールを使用する場合は設定方法が異なります。
関連情報の「WebViewerコントロール使用時にレポートパラメータを設定する方法」をご確認ください。
関連情報
旧文書番号
85307