ActiveReportsでは、スクリプトを使用することで、XMLファイルに保存されたレポートレイアウトにコードを含めることができます。RPXファイルをXMLファイルに保存するときにスクリプトを含めることで、後からデザイナを使用せずにレポートをロード、実行して、直接ビューワコントロールに表示できます。
このチュートリアルでは、スクリプトを使用して、サブレポートを含むスタンドアロンレポートを作成する方法を紹介します。
このトピックでは、以下のタスクを行います。
チュートリアルを完了すると、次のようなレポートが作成されます。
Visual StudioプロジェクトにActiveReportを追加する
rptMainレポートをデータソースに接続する
メモ: [レポートエクスプローラ]ノードの下にフィールドを表示するために、以下の手順を行います。
| SQLクエリ |
コードのコピー
|
|---|---|
SELECT * FROM [order details] inner join products on [order details].productid = products.productid |
|
サブレポートをレポートに追加する
サブレポートをデータソースに接続する
| SQLクエリ |
コードのコピー
|
|---|---|
SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID ORDER BY CompanyName, OrderDate |
|
rptMainレポートのレイアウトを作成する
| プロパティ名 | プロパティの値 |
|---|---|
| Name | ghCompanies |
| BackColor | LemonChiffon |
| CanShrink | True |
| DataField | CompanyName |
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | CompanyName |
| Size | 4, 0.2 in |
| Location | 0, 0 in |
| Font Bold | True |
| Font Size | 12 |
| プロパティ名 | プロパティの値 |
|---|---|
| Name | ghOrders |
| BackColor | LightYellow |
| CanShrink | True |
| DataField | OrderDate |
| GroupKeepTogether | All |
| KeepTogether | True |
TextBox1
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | OrderDate |
| Location | 1.1, 0 in |
| Size | 1, 0.2 in |
| OutputFormat | yyyy年M月d日 |
TextBox2
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | RequiredDate |
| Location | 3.5, 0 in |
| Size | 1, 0.2 in |
| OutputFormat | yyyy年M月d日 |
TextBox3
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | ShippedDate |
| Location | 5.5, 0 in |
| Size | 1, 0.2 in |
| OutputFormat | yyyy年M月d日 |
| Alignment | Right |
Label1
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 0, 0 in |
| Size | 1, 0.2 in |
| Text | 受注日: |
| Font Bold | True |
Label2
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 2.5, 0 in |
| Size | 1, 0.2 in |
| Text | 納期: |
| Font Bold | True |
Label3
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 4.8, 0 in |
| Size | 0.65, 0.2 in |
| Text | 発送日: |
| FontStyle | Bold |
| プロパティ名 | プロパティの値 |
|---|---|
| ReportName | C:\プロジェクトへのパス\rptSub.rpx |
| Name | SubReport1 |
| Size | 6.5, 1 in |
| Location | 0, 0 in |
サブレポートのレイアウトを作成する
| プロパティ名 | プロパティの値 |
|---|---|
| Name | ghOrderDetails |
| BackColor | LightSteelBlue |
| CanShrink | True |
| DataField | OrderID |
Label1
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 0, 0 in |
| Text | 商品名 |
| Font Bold | True |
| Alignment | Left |
Label2
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 3.25, 0 in |
| Text | 数量 |
| Font Bold | True |
| Alignment | Right |
Label3
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 4.4, 0 in |
| Text | 価格 |
| Font Bold | True |
| Alignment | Right |
Label4
| プロパティ名 | プロパティの値 |
|---|---|
| Location | 5.5, 0 in |
| Text | 値引き |
| Font Bold | True |
| Alignment | Right |
Line1
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 3.2 |
| X2 | 3.2 |
| Y1 | 0 |
| Y2 | 0.2 |
Line2
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 4.3 |
| X2 | 4.3 |
| Y1 | 0 |
| Y2 | 0.2 |
Line3
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 5.45 |
| X2 | 5.45 |
| Y1 | 0 |
| Y2 | 0.2 |
Line4
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 0 |
| X2 | 6.5 |
| Y1 | 0.2 |
| Y2 | 0.2 |
| プロパティ名 | プロパティの値 |
|---|---|
| BackColor | Gainsboro |
| CanShrink | True |
TextBox1
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | ProductName |
| Location | 0, 0 in |
| Size | 3.15, 0.2 in |
| Alignment | Left |
TextBox2
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | Quantity |
| Location | 3.25, 0 in |
| Size | 1, 0.2 in |
| Alignment | Right |
TextBox3
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | Products.UnitPrice |
| Location | 4.4, 0 in |
| Size | 1, 0.2 in |
| Alignment | Right |
| OutputFormat | Currency |
TextBox4
| プロパティ名 | プロパティの値 |
|---|---|
| DataField | Discount |
| Location | 5.5, 0 in |
| Size | 1, 0.2 in |
| Alignment | Right |
| OutputFormat | 0.00% |
Line5
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 3.2 |
| X2 | 3.2 |
| Y1 | 0 |
| Y2 | 0.2 |
Line6
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 4.3 |
| X2 | 4.3 |
| Y1 | 0 |
| Y2 | 0.2 |
Line7
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 5.45 |
| X2 | 5.45 |
| Y1 | 0 |
| Y2 | 0.2 |
Line8
| プロパティ名 | プロパティの値 |
|---|---|
| X1 | 0 |
| X2 | 6.5 |
| Y1 | 0.2 |
| Y2 | 0.2 |
rptMainレポートにスクリプトを追加する
Visual Basic
| Visual Basicコード(スクリプトエディタに貼り付けます) |
コードのコピー
|
|---|---|
Dim rptSub As GrapeCity.ActiveReports.SectionReport
Sub ActiveReport_ReportStart
'ジェネリックレポートの新しいインスタンスを作成します。
rptSub = new GrapeCity.ActiveReports.SectionReport()
'rpxファイルをジェネリックレポートにロードします。
rptSub.LoadLayout(me.SubReport1.ReportName)
'データをrptMainレポートに接続します。
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\ActiveReportsNET9\Data\NWIND.mdb;Persist Security Info=False"
Dim sqlString As String = "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName,OrderDate"
Dim ds As new GrapeCity.ActiveReports.Data.OleDBDataSource()
ds.ConnectionString = connString
ds.SQL = sqlString
rpt.DataSource = ds
End Sub
Sub Detail1_Format
Dim rptSubCtl As GrapeCity.ActiveReports.SectionReportModel.SubReport = me.SubReport1
Dim childDataSource As New GrapeCity.ActiveReports.Data.OleDBDataSource()
childDataSource.ConnectionString = CType(rpt.DataSource, GrapeCity.ActiveReports.Data.OleDBDataSource).ConnectionString
'SQLクエリでパラメータを設定します。
childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = <%OrderID%>"
'サブレポートにデータを渡します。
rptSub.DataSource = childDataSource
'rptSubをSubreportコントロールに表示します。
rptSubCtl.Report = rptSub
End Sub
|
|
C#
| C#コード(スクリプトエディタに貼り付けます) |
コードのコピー
|
|---|---|
GrapeCity.ActiveReports.SectionReport rptSub;
public void Detail1_Format()
{
GrapeCity.ActiveReports.SectionReportModel.SubReport rptSubCtl = this.SubReport1;
GrapeCity.ActiveReports.Data.OleDBDataSource childDataSource = new GrapeCity.ActiveReports.Data.OleDBDataSource();
childDataSource.ConnectionString = ((GrapeCity.ActiveReports.Data.OleDBDataSource) rpt.DataSource).ConnectionString;
//SQLクエリでパラメータを設定します。
childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = <%OrderID%>";
//サブレポートにデータを渡します。
rptSub.DataSource = childDataSource;
//rptSubをSubreportコントロールに表示します。
rptSubCtl.Report = rptSub;
}
public void ActiveReport_ReportStart()
{
//ジェネリックレポートの新しいインスタンスを作成します。
rptSub = new GrapeCity.ActiveReports.SectionReport();
//rpxファイルをジェネリックレポートにロードします。
rptSub.LoadLayout(this.SubReport1.ReportName);
//データをrptMainレポートに接続します。
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\ActiveReportsNET9\Data\NWIND.mdb;Persist Security Info=False";
string sqlString = "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName,OrderDate";
GrapeCity.ActiveReports.Data.OleDBDataSource ds = new GrapeCity.ActiveReports.Data.OleDBDataSource();
ds.ConnectionString = connString;
ds.SQL = sqlString;
rpt.DataSource = ds;
}
|
|
レポートを表示する
または