ActiveReports 9 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Data > Basic XML-Based Reports (RPX) |
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. By default, in ActiveReports, the NWind.mdb file is located in the [User Documents folder]\GrapeCity Samples\ActiveReports 9\Data folder. |
When you have finished this walkthrough, you get 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 create a layout for the report
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 | 1.5, 0.2in |
TextBox3
Property Name | Property Value |
---|---|
DataField | UnitsInStock |
Text | Stock |
Location | 4, 0in |
Size | 1, 0.2in |
To add scripting to the report to supply data for the controls
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=C:\Users\[User Folder]\Documents\GrapeCity Samples\ActiveReports 9\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=C:\Users\[User Folder]\Documents\GrapeCity Samples\ActiveReports 9\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
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 Detail1_Format if b then Me.Detail1.BackColor = Color.AliceBlue b= false else me.Detail1.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 Detail1_Format() { if(color) { this.Detail1.BackColor = System.Drawing.Color.AliceBlue; color = false; } else { this.Detail1.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.
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); |