ActiveReports supports field merged reports using the RichText control. The RichText control can contain field place holders that can be replaced with values (merged) at run time. This walkthrough illustrates how to create a mail-merge report using the RichText control.
This walkthrough is split up into the following activities:
When you complete this walkthrough you get a layout that looks similar to the following at design time and at run time.
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
SQL Query |
Copy Code
|
---|---|
SELECT Customers.CustomerID, Customers.CompanyName, Customers.ContactName, Customers.Address, Customers.City, Customers.Region, Customers.Country, Customers.PostalCode, Orders.OrderID, Orders.OrderDate, [Order Subtotals].Subtotal FROM Customers INNER JOIN ([Order Subtotals] INNER JOIN Orders ON [Order Subtotals].OrderID = Orders.OrderID) ON Customers.CustomerID = Orders.CustomerID |
To create a layout for the report
Property Name | Property Value |
---|---|
DataField | CustomerID |
Height | 2.5 |
KeepTogether | True |
Property Name | Property Value |
---|---|
Height | 1.1 |
KeepTogether | True |
NewPage | After |
Property Name | Property Value |
---|---|
Height | 0.8 |
BackColor | Coral |
Label
Property Name | Property Value |
---|---|
Location | 0, 0 in |
Size | 6.5, 0.65 in |
Text | GrapeCity |
Font > Size | 36 |
Font > Bold | True |
Property Name | Property Value |
---|---|
Location | 4, 0 in |
Size | 1, 0.2 in |
Name | txtSubtotal1 |
OutputFormat | Currency |
Visible | False |
SummaryType | SubTotal |
SummaryGroup | groupHeader1 |
RichTextBox
Property Name | Property Value |
---|---|
Location | 0, 0 in |
Size | 6.5, 2.1 in |
AutoReplaceFields | True |
Label1
Property Name | Property Value |
---|---|
Location | 0.875, 2.25 in |
Size | 1, 0.2 in |
Text | Order ID |
Font > Bold | True |
Label2
Property Name | Property Value |
---|---|
Location | 1.875, 2.25 in |
Size | 1, 0.2 in |
Text | Order Date |
Font > Bold | True |
Label3
Property Name | Property Value |
---|---|
Location | 4.375, 2.25 in |
Size | 1, 0.2 in |
Text | Amount |
Font > Bold | True |
Alignment | Right |
TextBox1 (OrderID)
Property Name | Property Value |
---|---|
Location | 0.875, 0 in |
Size | 1, 0.2 in |
TextBox2 (OrderDate)
Property Name | Property Value |
---|---|
Location | 1.875, 0 in |
Size | 1, 0.2 in |
OutputFormat | Date (MM/dd/yy) |
TextBox3 (Subtotal)
Property Name | Property Value |
---|---|
Location | 4.375, 0 in |
Size | 1, 0.2 in |
OutputFormat | Currency |
Alignment | Right |
Label1
Property Name | Property Value |
---|---|
Location | 5.15, 0.15 in |
Size | 1.35, 0.2 in |
Text | Best regards, |
Alignment | Right |
Label2
Property Name | Property Value |
---|---|
Location | 5.15, 0.8 in |
Size | 1.35, 0.2 in |
Text | Accounts Receivable |
To add fields to the RichText control
Paste into the RichText control |
Copy Code
|
---|---|
Dear [!ContactName], Thank you for your business. Below is a list of your orders for the past year with a total of [!SubTotal]. Please take this opportunity to review each order and total for accuracy. Call us at 1-800-DNT-CALL with any questions or concerns. |
To use the FetchData event to conditionally format data
To write the code in Visual Basic
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste JUST ABOVE the FetchData event. |
Copy Code
|
---|---|
Dim region As String |
Visual Basic.NET code. Paste INSIDE the FetchData event. |
Copy Code
|
---|---|
'If there is no region for the customer, display nothing If Fields("Region").Value Is System.DBNull.Value Then region = "" Else 'If there is a region, add a comma and a space region = ", " + Fields("Region").Value End If |
To write the code in C#
The following example shows what the code for the method looks like.
C# code. Paste JUST ABOVE the FetchData event. |
Copy Code
|
---|---|
string region; |
C# code. Paste INSIDE the FetchData event. |
Copy Code
|
---|---|
if(Fields["Region"].Value is System.DBNull) region = ""; else region = ", " + Fields["Region"].Value.ToString(); |
To add code to update RichText fields with the current date and conditional values
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Group Header Format event. |
Copy Code
|
---|---|
'Use the current date in the letter Me.RichTextBox1.ReplaceField("Date", System.DateTime.Today.Date.ToShortDateString()) 'Use the value returned by the FetchData event Me.RichTextBox1.ReplaceField("Region", region) |
To write the code in C#
C# code. Paste INSIDE the Group Header Format event. |
Copy Code
|
---|---|
//Use the current date in the letter this.richTextBox1.ReplaceField("Date", System.DateTime.Today.Date.ToShortDateString()); //Use the value returned by the FetchData event this.richTextBox1.ReplaceField("Region", region); |
To add code to send the group subtotal value to the RichText field
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Group Header BeforePrint event. |
Copy Code
|
---|---|
'Use the value from the hidden group subtotal field Me.RichTextBox1.ReplaceField("SubTotal", Me.txtSubtotal1.Text) |
To write the code in C#
C# code. Paste INSIDE the Group Header BeforePrint event. |
Copy Code
|
---|---|
//Use the value from the hidden group subtotal field this.richTextBox1.ReplaceField("SubTotal", this.txtSubtotal1.Text); |
To view the report
OR