ActiveReports for .NET 3 Online Help Request technical support
Walkthrough: Rich Text and Field Merging
See Also
User Guide > Samples and Walkthroughs > Walkthroughs > Standard Edition Walkthroughs > Advanced > RichText Walkthroughs > Walkthrough: Rich Text and Field Merging

Glossary Item Box

ActiveReports supports field merged reports using the RichText control. The RichText control can contain field placeholders that are replaceable with their 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:

To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.

When you complete this walkthrough, you will have a report that looks similar to the following:

Adding an ActiveReport to the Visual Studio project

To add an ActiveReport to your project

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item.
  3. Select ActiveReports 3.0 File and rename the file rptLetter.
  4. Click Open.

Connecting the report to a data source

To connect the report to a data source

  1. Click on the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  2. Click on Build.
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  4. Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
  6. In the Query field, type "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".
  7. Click OK to return to the report design surface.

Adding controls to the report to contain data

To add controls to the report

  1. Add a GroupHeader/Footer section to rptLetter by right-clicking the design surface of the report and selecting Insert > Group Header/Footer
  2. Make the following changes to the group header:
    • Change the Name property to ghCustomerID
    • Change the DataField property to CustomerID
    • Change the ColumnLayout property to False
    • Change the KeepTogether property to True
  3. Make the following changes to the group footer:
    • Change the Name property to gfCustomerID
    • Change the ColumnLayout property to False
    • Change the KeepTogether property to True
    • Change the NewPage property to After.
  4. Add the following controls to the PageHeader section:

    Control Name Text Location
    Picture imgLogo 3.8, 0
    Label lblNorthWind NorthWind 4.8, 0
    Label lblTraders Traders 4.8, 0.5

  5. Add the following controls to the GroupHeader section:

    Control DataField Name Text Miscellaneous Location
    RichText rtf 0, 0
    Textbox Subtotal txtTotalOrders Visible = False

    OutputFormat = Currency

    SummaryType = SubTotal

    SummaryGroup = ghCustomerID

    5.437, 0.0625
    Label lblOrderID Order ID 0.875, 2.25
    Label lblOrderDate Order Date 1.875, 2.25
    Label lblAmount Amount 4.375, 2.25

  6. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated. 

    Field Text Miscellaneous Location
    OrderID Order ID Alignnent: Right 0.8125, 0
    OrderDate Order Date OutputFormat = MM/dd/yy 1.875, 0
    Subtotal Subtotal OutputFormat = Currency

    Alignment: Right

    4.3125, 0

  7. Add the following controls to the GroupFooter section:

    Control Name Text Location
    Label lblYours Yours, 0.1875, 0.125
    Label lblNTAP Accounts Receivable 0.1875, 0.4375

  8. Add the following controls to the PageFooter section:

    Control Name Text Location
    Label lblNWAddress NorthWind Traders, One Portals Way, Twin Points WA 98156 0, 0.0625

Adding fields to the RichText control

To add fields to the RichText control

  1. Double-click inside the RichText control box to select it.
  2. Right-click inside the box and choose Insert Fields.
  3. Insert the following fields in the following order:
    • Date
    • CompanyName
    • ContactName
    • AddressLine
    • City
    • Region
    • Country
    • PostalCode
    • ContactName
    • TotalOrders
  4. Add the following text to the RichText control box: "Dear [!ContactName], Thank you for your business. Below is a list of your orders for the past year with a total of [!TotalOrders]. Please take this opportunity to review each order and total for accuracy. Call us at 1-800-DNT-CALL with any questions or concerns."
  5. Your RichText control should be arranged like the following.

Using the FetchData event to get information from the data source

To write the code in Visual Basic

To write the code in C#

Adding code to update the field values in the Rich Text control

To write the code in Visual Basic or C#

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

'Visual Basic
Private Sub ghCustomerID_Format(ByVal sender As Object, ByVal e As System.EventArgs) _
   Handles ghCustomerID.Format
    Me.rtf.ReplaceField("CompanyName", companyName)
    Me.rtf.ReplaceField("ContactName", contactName)
    Me.rtf.ReplaceField("AddressLine", addressLine)
    Me.rtf.ReplaceField("City", city)
    Me.rtf.ReplaceField("Region", region)
    Me.rtf.ReplaceField("Country", country)
    Me.rtf.ReplaceField("PostalCode", postalCode)
    Me.rtf.ReplaceField("Date", System.DateTime.Today.Date)
End Sub

//C#
private void ghCustomerID_Format(object sender, System.EventArgs eArgs)
{
   this.rtf.ReplaceField("CompanyName", companyName);
   this.rtf.ReplaceField("ContactName", contactName);
   this.rtf.ReplaceField("AddressLine", addressLine);
   this.rtf.ReplaceField("City", city);
   this.rtf.ReplaceField("Region", region);
   this.rtf.ReplaceField("Country", country);
   this.rtf.ReplaceField("PostalCode", postalCode);
   this.rtf.ReplaceField("Date", System.DateTime.Today.Date.ToString());
}

Adding code to the Group Header BeforePrint Event

To write the code in Visual Basic

To write the code in C#

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 Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer.

See Also

©2009. All Rights Reserved.