Glossary Item Box
ActiveReports gives you complete control to bind reports to any type of data source, including arrays, through its programmable object model. You can create a report without setting the report's data source and load the data from your data source into the report's controls at run time. The Fields property allows data binding between the controls and the run-time fields. It also allows the control's DataField property to be set to any of the run-time defined names. The DataInitialize and FetchData events are used to define the run-time fields and feed the data values of these fields so they can be used with unbound controls.
This walkthrough illustrates the fundamentals of using the DataInitialize and FetchData events to set up an unbound report.
This walkthrough is split 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 a report that looks similar to the following.
To add an ActiveReport to your project
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
Dim m_cnnString As String
Dim sqlString As String
Dim m_reader As OleDb.OleDbDataReader
Dim m_cnn As OleDb.OleDbConnection
Private Sub rptUnbound_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.ReportStart
m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ _ Data Dynamics\ActiveReports for .NET 2.0\Data\NWIND.MDB;Persist _ Security Info=False"
sqlString = "SELECT * FROM categories INNER JOIN products ON _ categories.categoryid = products.categoryid ORDER BY _ products.categoryid, products.productid"
m_cnn = New OleDb.OleDbConnection(m_cnnString)
Dim m_Cmd As New OleDb.OleDbCommand(sqlString, m_cnn)
If m_cnn.State = ConnectionState.Closed Then
m_cnn.Open()
End If
m_reader = m_Cmd.ExecuteReader()
End Sub
//C#
private OleDb.OleDbConnection m_cnn;
private OleDb.OleDbDataReader m_reader;
private void rptUnbound_ReportStart(object sender, System.EventArgs eArgs)
{
string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Data Dynamics\ActiveReports for .NET 2.0\Data\NWIND.MDB;Persist Security Info=False";
string sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid = products.categoryid ORDER BY products.categoryid, products.productid";
m_cnn = new OleDbConnection(m_cnnString);
OleDbCommand m_Cmd = new OleDbCommand(sqlString,m_cnn);
if(m_cnn.State == ConnectionState.Closed)
{
m_cnn.Open();
}
m_reader = m_Cmd.ExecuteReader();
}
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 rptUnbound_ReportEnd(ByVal sender As Object, ByVal e As System _
.EventArgs) Handles Me.ReportEnd
m_cnn.Close()
m_reader.Close()
End Sub
//C#
private void rptUnbound_ReportEnd(object sender, System.EventArgs e)
{
m_cnn.Close();
m_reader.Close();
}
To add controls to the report
Control | DataField | Name | Text/Caption | Location |
---|---|---|---|---|
TextBox | ProductName | txtProductName | Product Name | 0, 0.4 |
TextBox | UnitsInStock | txtUnitsInStock | Units in Stock | 4.75, 0.4 |
Label | (Empty string) | lblProductName | Product Name | 0, 0 |
TextBox | Description | txtDescription | Description | 1.8, 0.7 |
Label | (Empty string) | lblCategoryDescription | Category Description | 0, 0.7 |
Label | (Empty string) | lblUnitsInStock | Units in Stock | 4.75, 0 |
Label | (Empty string) | lblCategoryName | Category Name | 3.2, 0 |
TextBox | CategoryName | txtCategoryName | Category Name | 3.2, 0.4 |
Line | (Empty string) | Line1 | (Empty string) | X1 = 0
Y1 = 1 X2 = 6.5 Y2 = 1 |
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 rptUnbound_DataInitialize(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Me.DataInitialize
Fields.Add("CategoryName")
Fields.Add("ProductName")
Fields.Add("UnitsInStock")
Fields.Add("Description")
End Sub
//C#
private void rptUnbound_DataInitialize(object sender, System.EventArgs eArgs)
{
Fields.Add("CategoryName");
Fields.Add("ProductName");
Fields.Add("UnitsInStock");
Fields.Add("Description");
}
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 rptUnbound_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics._
ActiveReports.ActiveReport.FetchEventArgs) Handles MyBase.FetchData
Try
m_reader.Read()
Me.Fields("CategoryName").Value = m_reader("CategoryName")
Me.Fields("ProductName").Value = m_reader("ProductName")
Me.Fields("UnitsInStock").Value = m_reader("UnitsInStock")
Me.Fields("Description").Value = m_reader("Description")
eArgs.EOF = False
Catch ex As Exception
eArgs.EOF = True
End Try
End Sub
//C#
private void rptUnbound_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport.
FetchEventArgs eArgs)
{
try
{
m_reader.Read();
Fields["CategoryName"].Value = m_reader["CategoryName"].ToString();
Fields["ProductName"].Value = m_reader["ProductName"].ToString();
Fields["UnitsInStock"].Value = m_reader["UnitsInStock"].ToString();
Fields["Description"].Value = m_reader["Description"].ToString();
eArgs.EOF = false;
}
catch
{
eArgs.EOF = true;
}
}
Note: The DataInitialize and FetchData events are the only events in which the Fields collection should ever be referenced.
To view the report
See Also |
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.