ActiveReports 15 .NET Edition
ActiveReports 15 User Guide / Samples and Walkthroughs / Walkthroughs / Section Report Walkthroughs / Data / Basic XML-Based Reports (RPX)
In This Topic
    Basic XML-Based Reports (RPX)
    In This Topic

    ActiveReports 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. The NWIND.mdb file can be downloaded from GitHub: ..\Samples15\Data\NWIND.mdb.

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

    Design-Time Layout

    RPX report at design time

    RPX report at design time

    Run-Time Layout

    RPX report at run time

    RPX report at run time

    To 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 15 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 Basic Concepts for information on adding different report layouts.

    To create a layout for the report

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

      TextBox1

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

      TextBox2

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

      TextBox3

      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.

    To 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.

      To 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=[User Folder]\Samples14\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
      

      To 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=[User Folder]\Samples14\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();
      }
      

    To 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.

      To 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 Detail_Format
       if b then
        Me.Detail.BackColor = Color.AliceBlue
        b= false
        else
        me.Detail.BackColor = Color.Cyan
        b = true
       End If
      End Sub
      

      To write the script in C#

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

    Loading 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.

      To 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)
      

      To 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