ActiveReports Developer 7
Basic XML-Based Reports (RPX)
See Also Support Forum
ActiveReports Developer 7 > ActiveReports Developer Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Basic XML-Based Reports (RPX)

Glossary Item Box

ActiveReports Developer allows you to create reports with embedded script and save them to the XML-based RPX file format. By embedding script in reports saved as RPX files, you can later load, run, and display reports directly in the viewer control without rebuilding the application. This walkthrough illustrates how to create a simple report, using the XML-based report template.

This walkthrough is split into the following activities:

Note: This walkthrough uses the Northwind database. By default, in ActiveReports, the NWind.mdb file is located in the [User Documents folder]\ComponentOne Samples\ActiveReports Developer 7\Data folder.

When you have finished this walkthrough, you get a report that looks similar to the following at design time and at runtime.

Design Time Layout


Runtime Layout


ShowTo add an ActiveReport to the Visual Studio project

  1. Create a new Visual Studio project.
  2. From the Project menu, select Add New Item.
  3. In the Add New Item dialog that appears, select ActiveReports 7 Section Report (xml-based) and in the Name field, rename the file as rptScript.
  4. Click the Add button to open a new section report in the designer.
  5. In the Solution Explorer click the rptScript.rpx item and in the properties window set the Build Action property to Embedded Resource.

See Adding an ActiveReport to a Project for information on adding different report layouts.

ShowTo create a layout for the report

  1. In the Visual Studio toolbox, expand the ActiveReports 7 Section Report node and drag three TextBox controls onto the detail section and set the properties of each textbox as indicated:

    ShowTextBox1

    Property Name Property Value
    DataField ProductName
    Text Product Name
    Location 0, 0in
    Size 2.3, 0.2in

    ShowTextBox2

    Property Name Property Value
    DataField QuantityPerUnit
    Text Quantity
    Location 2.4, 0in
    Size 1.5, 0.2in

    ShowTextBox3

    Property Name Property Value
    DataField UnitsInStock
    Text Stock
    Location 4, 0in
    Size 1, 0.2in
  2. Click just below the fields to select the Detail section, and in the Properties Window, set the CanShrink property to True to eliminate white space in the rendered report.

ShowTo add scripting to the report to supply data for the controls

  1. Click the Script tab located at the bottom of the report designer to access the script editor.
  2. Add the scripting code.

    The following example shows what the scripting code looks like.

    Warning: Do not access the Fields collection outside the DataInitialize and FetchData events. Accessing the Fields collection outside of these events is not supported, and has unpredictable results.

    ShowTo write the script in Visual Basic.NET

    Visual Basic.NET script. Paste in the script editor window. Copy Code
    Private Shared m_cnn As System.Data.OleDb.OleDbConnection
    
    Public Sub ActiveReport_ReportStart()
      'Set up a data connection for the report
      Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\ComponentOne Samples\ActiveReports Developer 7\Data\NWIND.mdb"
      Dim sqlString As String = "SELECT * FROM products"
         
      m_cnn = new System.Data.OleDb.OleDbConnection(connString)
      Dim m_Cmd As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn)
       
      If m_cnn.State = System.Data.ConnectionState.Closed Then
         m_cnn.Open
      End If
      rpt.DataSource = m_Cmd.ExecuteReader
    End Sub
    
    Public Sub ActiveReport_ReportEnd()
      'Close the data reader and connection
      m_cnn.Close
    End Sub
    

    ShowTo write the script in C#

    C# script. Paste in the script editor window. Copy Code
    private static System.Data.OleDb.OleDbConnection m_cnn;
    
    public void ActiveReport_ReportStart()
    {
        //Set up a data connection for the report
        string m_cnnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\ComponentOne Samples\ActiveReports Developer 7\Data\NWIND.mdb";
        string sqlString = "SELECT * FROM products";
        m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString);
        System.Data.OleDb.OleDbCommand m_Cmd = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn);
     
        if(m_cnn.State == System.Data.ConnectionState.Closed)
        {
            m_cnn.Open();
        }
        rpt.DataSource = m_Cmd.ExecuteReader();
    }
    
    public void ActiveReport_ReportEnd()
    {
        //Close the data reader and connection
        m_cnn.Close();
    }
    

ShowTo add scripting to alternate colors in the detail section

  1. Click the Script tab located at the bottom edge of the report designer to access the scripting editor.
  2. Add the scripting code to set alternate colors in the rows of the detail section.

    The following example shows what the scripting code looks like.

    ShowTo write the script in Visual Basic.NET

    Visual Basic.NET script. Paste in the script editor window. Copy Code
    Dim b as boolean = true
    
    Sub Detail1_Format
     if b then
      Me.Detail1.BackColor = Color.AliceBlue
      b= false
      else
      me.Detail1.BackColor = Color.Cyan
      b = true
     End If
    End Sub
    

    ShowTo write the script in C#

    C# script. Paste in the script editor window. Copy Code
    bool color = true;
    public void Detail1_Format()
    {
      if(color)
      {
          this.Detail1.BackColor = System.Drawing.Color.AliceBlue;
          color = false;
      }
      else
      {
          this.Detail1.BackColor = System.Drawing.Color.Cyan;
          color = true;
      }
    }
    

ShowLoading the report to the Viewer

You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. You can also load the report to the Viewer control. 

  • Drag the ActiveReports Viewer control from the Visual Studio toolbox onto the Windows Form and set its Dock property to Fill.
  • Double-click the title bar of the Windows Form containing the viewer to create a Form_Load event and add the code needed to load the RPX into a generic ActiveReport and display it in the viewer.

    The following example shows what the code for the method looks like.

    ShowTo write the script in Visual Basic.NET

    Visual Basic.NET script. Paste INSIDE the Form_Load event. Copy Code
    Dim sectionReport As New GrapeCity.ActiveReports.SectionReport()
    Dim xtr As New System.Xml.XmlTextReader("..\..\rptScript.rpx")
    sectionReport.LoadLayout(xtr)
    xtr.Close()
    Viewer1.LoadDocument(sectionReport)
    

    ShowTo write the script in C#

    C# script. Paste INSIDE the Form_Load event. Copy Code
    GrapeCity.ActiveReports.SectionReport sectionReport = new GrapeCity.ActiveReports.SectionReport();
    System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(@"..\..\rptScript.rpx");
    sectionReport.LoadLayout(xtr);
    xtr.Close();
    viewer1.LoadDocument(sectionReport);
    

See Also

©2014. ComponentOne, a division of GrapeCity. All rights reserved.