Glossary Item Box

Samples | Walkthroughs | Scripting

See Also ActiveReports for .NET 2 Online Help Send feedback to Data Dynamics

Walkthrough: Scripting and Subreports

ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when the RPX files 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:

To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).

When you have finished this walkthrough, you will have a report that looks similar to the following.

Adding two ActiveReports to a Visual Studio project

To add two ActiveReports to a Visual Studio project

  1. Open a new project in Visual Studio.
  2. Click on Project > Add New Item.
  3. Select ActiveReports file and rename the file rptMain.
  4. Click Open.
  5. Click on Project > Add New Item.
  6. Select ActiveReports file and rename the file rptSub.
  7. Click Open.

Connecting the subreport to a data source

To connect the subreport to a data source

  1. In the design view of rptSub, click the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
  2. Click the Build... button.
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next >>
  4. Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
  6. In the Query field, type "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = <%orderID%>".
  7. Click OK to return to the report design surface.

Adding controls to the main report to display data

To add controls to rptMain

  1. Right-click anywhere on rptMain and select Insert > Group Header/Footer.
  2. Make the following changes to the group header:
    • Change the name to ghCompanies
    • Change the DataField to CompanyName
    • Change the GroupKeepTogether property to All
  3. Add a second GroupHeader/Footer section.
  4. Make the following changes to the group header:
    • Change the name to ghOrders
    • Change the DataField to OrderDate
    • Change the GroupKeepTogether property to All
  5. Add the following controls to rptMain, with properties set as indicated:

    Control DataField Name Text/Caption Section Location
    TextBox CompanyName txtCompanyName Company Name ghCompanies 0, 0
    TextBox OrderDate txtOrderDate Order Date ghOrders 1, 0
    TextBox RequiredDate txtRequiredDate Required Date ghOrders 3.5, 0
    TextBox ShippedDate txtShippedDate Shipped Date ghOrders 5.5, 0
    Label (Empty string) lblOrderDate Ordered: ghOrders 0, 0
    Label (Empty string) lblRequiredDate Required: ghOrders 2.5, 0
    Label (Empty string) lblShippedDate Shipped: ghOrders 4.5, 0
    Subreport (Empty string) Subreport1 (Empty string) Detail 0, 0
  6. Set the ReportName property of Subreport1 to rptSub.rpx and the Width property to 6.5 in.

Adding controls to the subreport to display data

To add controls to rptSub

  1. Change the ShowParameterUI property of rptSub to False. 
  2. Add a GroupHeader/Footer section to rptSub.
  3. Make the following changes to the group header:
    • Change the name to ghOrderDetails
    • Change the DataField to [Order Details].OrderID
  4. Add the following controls to the $$GroupHeader section$$:

    Control Name Text/Caption Location
    Label lblUnitPrice Unit Price 4.375, 0
    Label lblDiscount Discount 5.5, 0
    Label lblProductName Product Name 0, 0
    Label lblQuantity Quantity 3.25, 0
    Line Line (Empty string) X1 = 3.2

    Y1 = 0

    X2 = 3.2

    Y2 = 0.2

    Line Line1 (Empty string) X1 = 4.3

    Y1 = 0

    X2 = 4.3

    Y2 = 0.2

    Line Line2 (Empty string) X1 = 5.5

    Y1 = 0

    X2 = 5.5

    Y2 = 0.2

    Line Line3 (Empty string) X1 = 0

    Y1 = 0.2

    X2 = 6.5

    Y2 = 0.2

  5. Add the following controls to the Detail section:
    Control DataField Name Text/Caption Misc Details Location
    TextBox ProductName txtProductName Product Name (Empty string) 0, 0
    TextBox Quantity txtQuantity Quantity (Empty string) 3.25, 0
    TextBox order details.UnitPrice txtUnitPrice Unit Price OutputFormat = Currency 4.375, 0
    TextBox Discount txtDiscount Discount OutputFormat = Currency 5.5, 0
    Line (Empty string) Line4 (Empty string) (Empty string) X1 = 3.2

    Y1 = 0

    X2 = 3.2

    Y2 = 0.2

    Line (Empty string) Line5 (Empty string) (Empty string) X1 = 4.3

    Y1 = 0

    X2 = 4.3

    Y2 = 0.2

    Line (Empty string) Line6 (Empty string) (Empty string) X1 = 5.5

    Y1 = 0

    X2 = 5.5

    Y2 = 0.2

    Line (Empty string) Line7 (Empty string) (Empty string) X1 = 0

    Y1 = 0.2

    X2 = 6.5

    Y2 = 0.2

Adding the scripting code for rptMain

To add scripting to the report

  1. Below the report designer, you will find two tabs. The one on the left is for the Script view, and the one on the right is for the Design view. Click on the View Script tab.

  2. Add the following scripting code.

    The following example shows what the scripting code looks like.

    //C#
    private string getDatabasePath()
    {
    Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine;
    regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 2.0\\SampleDB");
    return (string)regKey.GetValue("");
    }
    public void ActiveReport_ReportStart()
    {
    string m_dbPath = getDatabasePath();
    string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_dbPath + "\\NWind.MDB";
    string sqlString = "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName, OrderDate";
    DataDynamics.ActiveReports.DataSources.OleDBDataSource m_ds = new DataDynamics .ActiveReports.DataSources.OleDBDataSource();
    m_ds.ConnectionString = m_cnnString;
    m_ds.SQL = sqlString;
    rpt.DataSource = m_ds;
    }
    public void Detail_Format()
    {
    DataDynamics.ActiveReports.ActiveReport rptSub = new DataDynamics.ActiveReports .ActiveReport();
    rptSub.LoadLayout(System.Windows.Forms.Application.StartupPath + @"..\..\" + ((SubReport)rpt.Sections["Detail"].Controls["SubReport"]) .ReportName);
    ((SubReport)rpt.Sections["Detail"].Controls["SubReport"]).Report = rptSub;
    }

Adding the scripting code for rptSub

To add scripting to the report

  1. Below the report designer, you will find two tabs. The one on the left is for the Script view, and the one on the right is for the Design view. Click on the View Script tab. Add the following scripting code.
  2. The following example shows what the scripting code looks like.

    //C#
    int cnt;
    public void Detail_Format()
    {
    cnt++;
    if(cnt % 2 == 0)
    {
    rpt.Sections["Detail"].BackColor = System.Drawing.Color.White;
    }
    else
    {
    rpt.Sections["Detail"].BackColor = System.Drawing.Color .BlanchedAlmond;
    }
    }
  3. Click OK to continue.

Viewing the report

To view the report

  1. Add the ActiveReports viewer control to a Windows Form.
  2. Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.

Samples | Walkthroughs | Scripting

 

 


Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.