Layout Data
Layout data is the data that is used by a DLEX file during the layout process. While the DLEX file remains constant and specifies how a document will be laid out, the layout data will change and determine the content that will be used for a specific layout. Layout data should use the NameValueLayoutData class whenever possible, but if advanced options are required, the EventDrivenLayoutData class can be used.
// Create the document's layout from a DLEX template
DocumentLayout layout = new DocumentLayout("SimpleReport.dlex");
// Specify the data to be used
NameValueLayoutData layoutData = new NameValueLayoutData();
layoutData.Add("PreparedFor", "Alex Smith");
layoutData.Add("Products", listOfProducts); // Any array or enumerable list of objects
// Layout the document and save the PDF
Document document = layout.Layout(layoutData);
document.Draw(outputFilePath);
' Create the document's layout from a DLEX template
Dim MyLayout As DocumentLayout = New DocumentLayout("SimpleReport.dlex")
' Specify the data to be used
Dim MyLayoutData As NameValueLayoutData = New NameValueLayoutData()
MyLayoutData.Add("PreparedFor", "Alex Smith")
MyLayoutData.Add("Products", MyListOfProducts) ' Any array or enumerable list of objects
' Layout the document And save the PDF
Dim MyDocument As Document = MyLayout.Layout(MyLayoutData)
MyDocument.Draw(outputFilePath)
Top Level Layout Data
There are two types of layout data that can be used by a DocumentLayout. The first is an object value that will appear consistently throughout the document (on fixed pages or reports). The PreparedFor value in the above example is an example of this. This type of data can be accessed from anywhere in the layout document by specifying it in the dataName property of a record box or by putting it within pound signs in a record area text property (e.g. "This was prepared for #PreparedFor#.").
Layout Data for Reports
The other type of data, is enumerable data or a list of data that will be used by a report in the layout document. This can be an enumerable list or array of objects (or data from a database as shown in the section below). The data used by a report will be specified with its dataName property ("Products" in the example above). Layout Elements that are in the report's detail section will then access the current object or record with a DataName that matches the property or field name for that record. Layout Elements that are in a reports header or footer, will display the data for the first object or record. If a Layout Element specifies data that is not on the object, a value by that name will try retrieved recursively from its parents (if it is on a sub report) or from a top level data value from the layout data. For example, if a RecordBox in the details of a report has its DataName property set to "PreparedFor", the value will first try to be retrieved from the report's data and if it is not found there, will be retrieved from the layout data.
Using Layout Data from a Database
Layout Data for a Report can also come from a database by using the DataReaderReportData or DataTableReportData classes. An example is shown below:
// Create a database connection
string connectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=true";
SqlConnection connection = new SqlConnection(connectionString);
Document document = null;
using (connection)
{
// Create the document's layout from a DLEX template
DocumentLayout layoutReport = new DocumentLayout("SimpleReportWithCoverPage.dlex");
// Query the database
string sql = "SELECT ProductName, QuantityPerUnit, UnitPrice FROM Products";
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Specify the data.
NameValueLayoutData layoutData = new NameValueLayoutData();
layoutData.Add("ReportCreatedFor", "Alex Smith");
layoutData.Add("Products", new DataReaderReportData(connection, reader));
// Layout the document
document = layoutReport.Layout(layoutData);
}
// Save the PDF to a file
document.Draw(outputFilePath);
' Create a database connection
Dim MyConnectionString As String
Set MyConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=true"
Dim Myconnection As SqlConnection = New SqlConnection(connectionString)
Dim MyDocument As Document = Nothing
' Create the document's layout from a DLEX template
Dim MyLayoutReport As DocumentLayout = New DocumentLayout("SimpleReportWithCoverPage.dlex")
' Query the database
Dim MySql As String
Set MySql = "SELECT ProductName, QuantityPerUnit, UnitPrice FROM Products"
Dim MyCommand As SqlCommand = New SqlCommand(sql, connection)
Myconnection.Open()
Dim MyReader As SqlDataReader = MyCommand.ExecuteReader()
' Specify the data.
Dim MylayoutData As NameValueLayoutData = New NameValueLayoutData()
MylayoutData.Add("ReportCreatedFor", "Alex Smith")
MylayoutData.Add("Products", New DataReaderReportData(connection, reader))
' Layout the document
MyDocument = MyLayoutReport.Layout(MylayoutData)
Myconnection.Close()
' Save the PDF to a file
MyDocument.Draw(outputFilePath);