How to: Create Calculated Field
This example demonstrates how to create a calculated field in the Snap Report API.
- Load the document template by calling the IRichEditDocumentServer.LoadDocumentTemplate method.
- Call the SubDocument.BeginUpdate method to enable the document modification.
- Create a new CalculatedField object. Pass the name for the created field and data member to be associated with to the object’s constructor method.
- To associate the calculated field with the data source, use the CalculatedField.DataSourceName property.
- Specify the type and expression for the calculated field by setting the CalculatedField.FieldType and CalculatedField.Expression properties.
- Add the created CalculatedField object to the document collection of calculated fields. To do that, access the collection through the IDataSourceOwner.CalculatedFields property and call the Add method.
- To finalize the modification, call the SubDocument.EndUpdate method.
- Update the document fields by calling the FieldCollection.Update method.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/snap-document-server-api-t429162
server.LoadDocumentTemplate("Template.snx")
server.Document.BeginUpdate()
Dim field As New CalculatedField("newField", "Products")
field.FieldType = DevExpress.XtraReports.UI.FieldType.Int32
field.Expression = "[UnitsInStock]*[UnitPrice]"
field.DataSourceName = "Data Source 1"
server.Document.CalculatedFields.Add(field)
server.Document.EndUpdate()
server.Document.Fields.Update()