Glossary Item Box

Samples | Walkthroughs

See Also ActiveReports for .NET 2 Online Help Send feedback to Data Dynamics

Creating and Modifying Report Layouts at Run Time 2003

ActiveReports objects and controls are completely accessible at run time. The properties of any of the report sections or controls can be modified to produce a dynamic view of the report. The format event allows the properties of report sections and controls to be modified including height, visibility and other visual properties. The format event is the only event in which the printable area of a section can be modified. Once this event is completed, any changes to the section's height will not be reflected in the report output.

 

Note: Controls are added dynamically in the ReportStart event. Controls added dynamically after the ReportStart event cause problems.
This walkthrough illustrates how to create a report layout at run time based on user input using Visual Studio 2003.

 

This walkthrough is split up into the following activities:

To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).

When you have completed this walkthrough, you will have output that looks similar to the following.

CREATING A USER INTERFACE

Adding controls to a Windows Form

To add controls to the form

Generating a dataset for the Windows Form

To generate a dataset for the Windows Form

  1. On the Data toolbox, double-click OleDbDataAdapter.
  2. This adds the adapter to the Windows Form and opens the Data Adapter Configuration Wizard.
  3. Follow the instructions to choose the data connection, select the data provider, select the database and enter the SQL statement, "SELECT CategoryID, Discontinued, ProductID, ProductName, QuantityPerUnit, ReorderLevel, SupplierID, UnitPrice, UnitsInStock, UnitsOnOrder FROM Products" (For help with this, see "Connecting the report to a data source" below.)
  4. Click "Finish" to return to the Windows Form.
  5. From the Visual Studio Data menu, select Generate Dataset...
  6. This opens the Generate Dataset dialog.
  7. Select New and select the Products table check box.
  8. Check "Add this dataset to the designer" and click OK.

ADDING AN ACTIVEREPORT TO THE PROJECT

To add an ActiveReport to your project

  1. From the Project menu, select Add New Item..., select ActiveReports File, and name the file rptFieldsRT.
  2. Click Open.

Connecting the report to a data source

To connect the report to a data source

  1. Click on the yellow report DataSource icon in the Detail section to open the Report Data Source dialog.
  2. Click the Build... button.
  3. Select "Microsoft Jet 4.0 OLE DB Provider" and click the Next >> button.
  4. Click the ellipsis button to navigate to Nwind.mdb and click the Open button.
  5. Click OK to continue.
  6. In theQuery field, type "Select * from products".
  7. Click OK to return to the report design surface.

Creating an array list and adding controls to the report at run time

To create an array list and add controls at run time

In the code view of rptFieldsRT, insert the following code beneath "ActiveReports Designer generated code."

' Visual Basic
Private m_arrayFields As ArrayList
Public WriteOnly Property FieldsList() As ArrayList
        Set(ByVal Value As ArrayList)
                m_arrayFields = Value
        End Set
End Property
Private m_defaultHeight As Single = 0.2F
Private m_defaultWidth As Single = 4.0F
Private m_currentY As Single = 0.0F
Private Sub constructReport()
        Try
                Me.Detail.CanGrow = True
                Me.Detail.CanShrink = True
                Me.Detail.KeepTogether = True
                Dim i As Integer
                For i = 0 To m_arrayFields.Count - 1
                        If m_arrayFields(i).ToString <> "CategoryID" Then
                                Dim lbl As New Label()
                                lbl.Text = m_arrayFields(i) + ":"
                                lbl.Location() = New System.Drawing.PointF _
                                        (0.0F, m_currentY)
                                lbl.Width = 0.9F
                                lbl.Height = m_defaultHeight
                                Me.Detail.Controls.Add(lbl)
                                Dim txt As New TextBox()
                                txt.DataField = m_arrayFields(i)
                                txt.Location = New System.Drawing.PointF _
                                        (1.0F, m_currentY)
                                txt.Width = m_defaultWidth
                                txt.Height = m_defaultHeight
                                Me.Detail.Controls.Add(txt)
                                If m_arrayFields(i) = "UnitPrice" Then
                                        txt.OutputFormat = "$#.00"
                                End If
                                m_currentY = m_currentY + m_defaultHeight
                        End If
                Next
        Catch ex As Exception
                System.Windows.Forms.MessageBox.Show _
                        ("Error in Report-constructReport: " + ex.Message, "Project Error", _
                        System.Windows.Forms.MessageBoxButtons.OK,  _
                        System.Windows.Forms.MessageBoxIcon.Error)
        End Try
End Sub
//C#
private System.Collections.ArrayList m_arrayFields;
public System.Collections.ArrayList FieldsList
{
        set{m_arrayFields = value;}
}
float m_defaultHeight = .2f;
float m_defaultWidth = 4f;
float m_currentY = 0f;
private void constructReport()
{
        try
        {
                this.Detail.CanGrow = true;
                this.Detail.CanShrink = true;
                this.Detail.KeepTogether = true;
                for(int i=0;i<m_arrayFields.Count;i++)
                {
                        if(m_arrayFields[i].ToString() != "CategoryID")
                        {
                                Label lbl = new Label();
                                lbl.Text = m_arrayFields[i].ToString() + ":";
                                lbl.Location = new System.Drawing.PointF
                                        (0f,m_currentY);
                                lbl.Width =.9f;
                                lbl.Height = m_defaultHeight;
                                this.Detail.Controls.Add(lbl);
                                TextBox txt = new TextBox();
                                txt.DataField = m_arrayFields[i].ToString();
                                txt.Location = new System.Drawing.PointF
                                        (1f,m_currentY);
                                txt.Width = m_defaultWidth;
                                txt.Height = m_defaultHeight;
                                this.Detail.Controls.Add(txt);
                                m_currentY = m_currentY + m_defaultHeight;
                        }
                }
        }
        catch(Exception ex)
        {
                System.Windows.Forms.MessageBox.Show("Error in Report-constructReport: "
                        + ex.Message,"Project Error",System.Windows.Forms
                        .MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
        }
}

Adding code to the ReportStart event to call the constructReport method

To write the code in Visual Basic

To write the code in C#

Adding code to the Detail Format event to alternate colors in the report

To write the code in Visual Basic

To write the code in C#

ADDING CODE TO THE WINDOWS FORM

Creating a method to fill the check list box with field values

To create a method to fill the check list box

Adding code to the Form Load event to call the fillCheckBox method

To write the code in Visual Basic

To write the code in C#

Adding code to retrieve user input from the check list box

To write the code in Visual Basic

To write the code in C#

Creating a method to launch the report

To create a method to launch the report

Adding code for the button Click event to populate the array list and pass it to the report

To write the code in Visual Basic

To write the code in C#

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

' Visual Basic
Private Sub btnGenRpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnGenRpt.Click
Me.m_arrayField.Clear()
For i = 0 To Me.clbFields.CheckedItems.Count - 1
m_arrayField.Add(Me.clbFields.CheckedItems(i).ToString)
Next
launchReport()
End Sub
//C#
private void btnGenRpt_Click(object sender, System.EventArgs e)
{
this.m_arrayField.Clear();
for(int i=0;i<this.clbFields.CheckedItems.Count;i++)
{
m_arrayField.Add(this.clbFields.CheckedItems[i].ToString());
}
launchReport();
}

Selecting fields and viewing the report

To select fields and view the report

Samples | Walkthroughs

 

 


Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.