Add data at design time using the Report Explorer.
- In the Report Explorer, expand the Fields node.
- Right-click the Calculated node and select Add.
- With the field selected in the Properties window, enter a Formula for the calculation (for example, UnitPrice * Discount).

Note: Do not use an equals sign (=) at the beginning of the formula as you would in the DataField property of a textbox. - Drag the calculated field onto the design surface of the report to create a bound textbox.
- In the Report Explorer, right-click the Parameters node and select Add.
- With the parameter selected in the Properties window, enter a DefaultValue for the parameter.
- Select whether to PromptUser for a parameter value, and if so, supply a string for the Prompt.
Bind a report to a data source using the data source icon in the detail section band which opens the Report Data Source window. There are four tabs on the window for the four most commonly used data sources:
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the OLE DB tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
- Click the ellipsis (...) button to browse to your database or the sample Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter a SQL query to select the data that you want.
- Click OK to save the data source and return to the report design surface.
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the SQL tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft OLE DB Provider for SQL Server and click the Next button.
- Under 1. Select or enter a server name, drop down the box and select your server.
- Under 2. Enter information to log on to the server, set up your log on information.
- Under 3. you can select a database on the server or attach a database file.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter a SQL query to select the data that you want.
- Click OK to save the data source and return to the report design surface.
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the XML tab, next to File URL, click the .. button.
- In the Open File window that appears, navigate to your XML data file, select it, and click the Open button (the sample xml data file is located in C:\Program Files\GrapeCity\ActiveReports 6\Data\customer.xml; on a 64-bit Windows operating system the file is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\customer.xml).
- In the Recordset Pattern field, enter a valid XPath expression. (for example, //CUSTOMER for the sample xml data file)
- Click OK to save the data source and return to the report design surface.
- Double-click in the gray area below the design area of your report to create an event-handling method for the ReportStart event.
- Add code to:
- Change the data source at run time
- Close the data connection
- Add fields to the report
- Populate fields in the report
To create a data source in Visual Basic.NET
The following example shows what the code for the method looks like.
| Visual Basic.NET code. Paste JUST ABOVE the ReportStart event. | Copy Code |
|---|---|
Dim conn As System.Data.OleDb.OleDbConnection Dim reader As System.Data.OleDb.OleDbDataReader | |
| Visual Basic.NET code. Paste INSIDE the ReportStart event. | Copy Code |
|---|---|
Dim dbPath As String = getDatabasePath() Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\NWIND.mdb" conn = New System.Data.OleDb.OleDbConnection(connString) Dim cmdText As String = "SELECT Categories.*, Products.* FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE Products.UnitPrice = 18" | |
The following example shows what the code for the method looks like.
| C# code. Paste JUST ABOVE the ReportStart event. | Copy Code |
|---|---|
private static System.Data.OleDb.OleDbConnection conn; private static System.Data.OleDb.OleDbDataReader reader; | |
| C# code. Paste INSIDE the ReportStart event. | Copy Code |
|---|---|
string dbPath = getDatabasePath(); string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\NWIND.mdb"; conn = new System.Data.OleDb.OleDbConnection(connString); string cmdText = "SELECT Categories.*, Products.* FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE Products.UnitPrice = 18"; System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(cmdText, conn); conn.Open(); reader = cmd.ExecuteReader(); | |
To close the data connection in Visual Basic
- In design view of YourReportName, drop down the field at the top left of the code view and select (YourReportName Events).
- Drop down the field at the top right of the code view and select ReportEnd. This creates an event-handling method for ReportEnd event.
- Add code to the handler to close the data connection.
The following example shows what the code for the method looks like.
| Visual Basic.NET code. Paste INSIDE the ReportEnd event. | Copy Code |
|---|---|
reader.Close() conn.Close() | |
To close the data connection in C#
- Click in the gray area below YourReportName to select the report.
- Click the events icon in the Properties Window to display available events for the report.
- Double-click ReportEnd. This creates an event-handling method for the ReportEnd event.
- Add code to the handler to close the data connection.
The following example shows what the code for the method looks like.
| C# code. Paste INSIDE the ReportEnd event. | Copy Code |
|---|---|
reader.Close(); conn.Close(); | |
![]() |
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. |
- Right-click in any section of the design surface of the report, and select View Code to display the code view for the report.
- At the top left of the code view of the report, click the drop-down arrow and select (YourReportName Events).
- At the top right of the code window, click the drop-down arrow and select DataInitialize. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's Fields collection.
The following example shows what the code for the method looks like.
| Visual Basic.NET code. Paste INSIDE the DataInitialize event. | Copy Code |
|---|---|
Fields.Add("CategoryID")
Fields.Add("CategoryName")
Fields.Add("ProductName")
Fields.Add("UnitsInStock")
Fields.Add("Description")
Fields.Add("TotalLabel")
| |
- Click in the gray area below the report to select it.
- Click the events icon in the Properties Window to display available events for the report.
- Double-click DataInitialize. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's Fields collection.
The following example shows what the code for the method looks like.
| C# code. Paste INSIDE the DataInitialize event. | Copy Code |
|---|---|
Fields.Add("CategoryID");
Fields.Add("CategoryName");
Fields.Add("ProductName");
Fields.Add("UnitsInStock");
Fields.Add("Description");
Fields.Add("TotalLabel");
| |
To populate fields in Visual Basic
- At the top left of the code view for the report, click the drop-down arrow and select (YourReportName Events).
- At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
The following example shows what the code for the method looks like.
| Visual Basic.NET code. Paste INSIDE the FetchData event. | Copy Code |
|---|---|
Try
reader.Read()
Me.Fields("CategoryID").Value = reader("categories.CategoryID")
Me.Fields("CategoryName").Value = reader("CategoryName")
Me.Fields("ProductName").Value = reader("ProductName")
Me.Fields("UnitsInStock").Value = reader("UnitsInStock")
Me.Fields("Description").Value = reader("Description")
Me.Fields("TotalLabel").Value = "Total Number of " + reader("CategoryName") + ":"
eArgs.EOF = False
Catch
eArgs.EOF = True
End Try
| |
- Back in design view, click in the gray area below the report to select it.
- Click the events icon in the Properties window to display available events for the report.
- Double-click FetchData. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
The following example shows what the code for the method looks like.
| C# code. Paste INSIDE the FetchData event. | Copy Code |
|---|---|
try
{
reader.Read();
Fields["CategoryID"].Value = reader["categories.CategoryID"].ToString();
Fields["CategoryName"].Value = reader["CategoryName"].ToString();
Fields["ProductName"].Value = reader["ProductName"].ToString();
Fields["UnitsInStock"].Value = reader["UnitsInStock"].ToString();
Fields["Description"].Value = reader["Description"].ToString();
Fields["TotalLabel"].Value = "Total Number of " + reader["CategoryName"].ToString() + ":";
eArgs.EOF = false;
}
catch
{
eArgs.EOF = true;
}
| |
To use the IEnumerable data source
- Double-click in the gray area below the design area of your report to display the code view for the report.
- Add the following code as shown in the examples below.
To create a data source in Visual Basic Paste INSIDE the class declaration of the report
Copy CodePrivate datasource1 As IEnumerator(Of String) = Nothing Dim list As List(Of String) = Nothing
Paste INSIDE the class declaration of the report
Copy CodePrivate Function GetIEnumerableData() As IEnumerable(Of String) For i As Integer = 1 To 10 list.Add(String.Format("TestData_{0}", i.ToString())) Next Return list End FunctionPaste JUST ABOVE the InitializeComponent method
Copy Codeprivate IEnumerator<string> datasource = null;
Paste BELOW the InitializeComponent method
Copy Codeprivate IEnumerable<string> GetIEnumerableData() { for (int i = 1; i <= 10; i++) { yield return string.Format("TestData_{0}", i.ToString()); } } - Back in design view, click in the gray area below the report to select it.
- Click the events icon in the Properties window to display available events for the report.
- Double-click DataInitialize. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's Fields collection.
Paste INSIDE the DataInitialize event
Copy CodeMe.Fields.Add("TestField") Me.list = New List(Of String) datasource1 = GetIEnumerableData().GetEnumerator()Paste INSIDE the DataInitialize event
Copy Codethis.Fields.Add("TestField"); datasource = GetIEnumerableData().GetEnumerator(); - Back in design view, click in the gray area below the report to select it.
- Click the events icon in the Properties window to display available events for the report.
- Double-click FetchData. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
To populate fields in Visual Basic Paste INSIDE the FetchData event
Copy CodeIf datasource1.MoveNext() Then Me.Fields("TestField").Value = datasource1.Current eArgs.EOF = False Else eArgs.EOF = True End IfPaste INSIDE the FetchData event
Copy Codeif (datasource.MoveNext()) { this.Fields["TestField"].Value = datasource.Current; eArgs.EOF = false; } else eArgs.EOF = true;
Hide All