ActiveReports 9 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Script > Script for Subreports |
ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when reports are saved into XML, the reports later can be loaded, run, and displayed directly to the viewer control without needing to use the designer.
This walkthrough illustrates how to use scripting when creating a subreport.
This walkthrough is split up into the following activities:
Tip: For basic steps like adding a report to a Visual Studio project and viewing a report, please see the Basic Data Bound Reports walkthrough. |
Note: This walkthrough uses the Northwind database. By default, in ActiveReports, the Northwind.mdb file is located at [User Documents folder]\GrapeCity Samples\ActiveReports 9\Data\NWIND.mdb. |
When you have finished this walkthrough, you will have a report that looks similar to the following at design time and at runtime.
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the report to a data source
Note: The following steps are just for convenience so that the fields list in the Report Explorer can be populated at design time. |
SQL Query |
Copy Code
|
---|---|
SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID ORDER BY CompanyName, OrderDate |
To add a report for the subreport
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the subreport to a data source
Note: The following steps are just for convenience so that the fields list in the Report Explorer can be populated at design time. |
SQL Query |
Copy Code
|
---|---|
SELECT * FROM [order details] inner join products on [order details].productid = products.productid |
To create a layout for the main report
Property Name | Property Value |
---|---|
Name | ghCompanies |
BackColor | LemonChiffon |
CanShrink | True |
DataField | CompanyName |
GroupKeepTogether | All |
KeepTogether | True |
Property Name | Property Value |
---|---|
Size | 4, 0.2 in |
Location | 0, 0 in |
Font Bold | True |
Font Size | 12 |
Property Name | Property Value |
---|---|
Name | ghOrders |
BackColor | LightYellow |
CanShrink | True |
DataField | OrderDate |
GroupKeepTogether | All |
KeepTogether | True |
TextBox1
Property Name | Property Value |
---|---|
DataField | OrderDate |
Location | 1.1, 0 in |
Size | 1, 0.2 in |
OutputFormat | MM/dd/yy |
TextBox2
Property Name | Property Value |
---|---|
DataField | RequiredDate |
Location | 3.5, 0 in |
Size | 1, 0.2 in |
OutputFormat | MM/dd/yy |
TextBox3
Property Name | Property Value |
---|---|
DataField | ShippedDate |
Location | 5.5, 0 in |
Size | 1, 0.2 in |
OutputFormat | MM/dd/yy |
Alignment | Right |
Label1
Property Name | Property Value |
---|---|
Location | 0, 0 in |
Size | 1, 0.2 in |
Text | Ordered: |
Font Bold | True |
Label2
Property Name | Property Value |
---|---|
Location | 2.5, 0 in |
Size | 1, 0.2 in |
Text | Required: |
Font Bold | True |
Label3
Property Name | Property Value |
---|---|
Location | 4.8, 0 in |
Size | 0.65, 0.2 in |
Text | Shipped: |
FontStyle | Bold |
Property Name | Property Value |
---|---|
ReportName | C:\full project path\rptSub.rpx |
Name | SubReport1 |
Size | 6.5, 1 in |
Location | 0, 0 in |
GroupKeepTogether | All |
KeepTogether | True |
To create a layout for the subreport
Property Name | Property Value |
---|---|
Name | ghOrderDetails |
BackColor | LightSteelBlue |
CanShrink | True |
DataField | OrderID |
Label1
Property Name | Property Value |
---|---|
Location | 0, 0 in |
Text | Product Name |
Font Bold | True |
Alignment | Left |
Label2
Property Name | Property Value |
---|---|
Location | 3.25, 0 in |
Text | Quantity |
Font Bold | True |
Alignment | Right |
Label3
Property Name | Property Value |
---|---|
Location | 4.4, 0 in |
Text | Unit Price |
Font Bold | True |
Alignment | Right |
Label4
Property Name | Property Value |
---|---|
Location | 5.5, 0 in |
Text | Discount |
Font Bold | True |
Alignment | Right |
Line1
Property Name | Property Value |
---|---|
X1 | 3.2 |
X2 | 3.2 |
Y1 | 0 |
Y2 | 0.2 |
Line2
Property Name | Property Value |
---|---|
X1 | 4.3 |
X2 | 4.3 |
Y1 | 0 |
Y2 | 0.2 |
Line3
Property Name | Property Value |
---|---|
X1 | 5.45 |
X2 | 5.45 |
Y1 | 0 |
Y2 | 0.2 |
Line4
Property Name | Property Value |
---|---|
X1 | 0 |
X2 | 6.5 |
Y1 | 0.2 |
Y2 | 0.2 |
Property Name | Property Value |
---|---|
BackColor | Gainsboro |
CanShrink | True |
TextBox1
Property Name | Property Value |
---|---|
DataField | ProductName |
Location | 0, 0 in |
Size | 3.15, 0.2 in |
Alignment | Left |
TextBox2
Property Name | Property Value |
---|---|
DataField | Quantity |
Location | 3.25, 0 in |
Size | 1, 0.2 in |
Alignment | Right |
TextBox3
Property Name | Property Value |
---|---|
DataField | Products.UnitPrice |
Location | 4.4, 0 in |
Size | 1, 0.2 in |
Alignment | Right |
OutputFormat | Currency |
TextBox4
Property Name | Property Value |
---|---|
DataField | Discount |
Location | 5.5, 0 in |
Size | 1, 0.2 in |
Alignment | Right |
OutputFormat | Percentage |
Line5
Property Name | Property Value |
---|---|
X1 | 3.2 |
X2 | 3.2 |
Y1 | 0 |
Y2 | 0.2 |
Line6
Property Name | Property Value |
---|---|
X1 | 4.3 |
X2 | 4.3 |
Y1 | 0 |
Y2 | 0.2 |
Line7
Property Name | Property Value |
---|---|
X1 | 5.45 |
X2 | 5.45 |
Y1 | 0 |
Y2 | 0.2 |
Line8
Property Name | Property Value |
---|---|
X1 | 0 |
X2 | 6.5 |
Y1 | 0.2 |
Y2 | 0.2 |
To embed script in the main report
The following example shows what the script looks like.
To write the script in Visual Basic.NET
Visual Basic.NET script. Paste in the script editor window. |
Copy Code
|
---|---|
Dim rptSub As GrapeCity.ActiveReports.SectionReport
Sub ActiveReport_ReportStart
'Create a new instance of the generic report
rptSub = new GrapeCity.ActiveReports.SectionReport()
'Load the rpx file into the generic report
rptSub.LoadLayout(me.SubReport1.ReportName)
'Connect data to the main report
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\GrapeCity Samples\ActiveReports 9\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.SubReport = me.SubReport1
Dim childDataSource As New GrapeCity.ActiveReports.Data.OleDBDataSource()
childDataSource.ConnectionString = CType(rpt.DataSource, GrapeCity.ActiveReports.Data.OleDBDataSource).ConnectionString
'Set a parameter in the SQL query
childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = <%OrderID%>"
'Pass the data to the subreport
rptSub.DataSource = childDataSource
'Display rptSub in the subreport control
rptSubCtl.Report = rptSub
End Sub
|
To write the script in C#
C# code. Paste in the script editor window. |
Copy Code
|
---|---|
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;
//Set a parameter in the SQL query
childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = <%OrderID%>";
//Pass the data to the subreport
rptSub.DataSource = childDataSource;
//Display rptSub in the subreport control
rptSubCtl.Report = rptSub;
}
public void ActiveReport_ReportStart()
{
//Create a new instance of the generic report
rptSub = new GrapeCity.ActiveReports.SectionReport();
//Load the rpx file into the generic report
rptSub.LoadLayout(this.SubReport1.ReportName);
//Connect data to the main report
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\GrapeCity Samples\ActiveReports 9\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;
}
|
To view the report
OR