Back to Devexpress

CalculatedField.GetValue Event

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-calculatedfield-860eb8fb.md

latest6.5 KB
Original Source

CalculatedField.GetValue Event

Occurs when a calculated field obtains its value, enabling you to return a custom value for this calculated field.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public event GetValueEventHandler GetValue
vb
Public Event GetValue As GetValueEventHandler

Event Data

The GetValue event's data class is GetValueEventArgs. The following properties provide information specific to this event:

PropertyDescription
ReportProvides access to a report which owns the calculated field, for which the CalculatedField.GetValue event was raised.
RowProvides access to a data row whose data can be used to calculate a calculated field‘s custom value in the CalculatedField.GetValue event handler.
ValueGets or sets a value of a calculated field.

The event data class exposes the following methods:

MethodDescription
GetColumnValue(String)Returns the value of a specified column.

Remarks

Although the CalculatedField.Expression property supports numerous standard functions, operators, and constants, for some tasks these may not be enough. Handle the GetValue event to make a calculated field return a value based on your custom expression.

The GetValueEventArgs class introduces the GetValueEventArgs.Value property, which accepts the final calculated value for a calculated field, and the GetValueEventArgs.Row and GetValueEventArgs.Report properties, which enable you to obtain any information required to calculate a value for a calculated field.

Example

The report created in this example is bound to the “Orders” table of the sample Northwind database.

To sort records by days of the week at runtime, do the following.

  1. Create a calculated field with the CalculatedField.FieldType property set to String. Add a GroupHeader band, and add a grouping criteria (with the GroupField.FieldName property set to the created calculated field) to its GroupHeaderBand.GroupFields collection.
  2. Then, drop the calculated field from the Field List onto the GroupHeader1 band for the XRLabel control (named hdrLabel ) bound to the field to be automatically created.
  3. Handle the calculated field’s CalculatedField.GetValue and the hdrLabel ‘s XRControl.BeforePrint event handlers in the following way.
csharp
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
// ...

private void calculatedField1_GetValue(object sender, GetValueEventArgs e) {
    object columnValue = e.GetColumnValue("OrderDate");
    e.Value = (int)((DateTime)columnValue).DayOfWeek;
}

private void hdrLabel_BeforePrint(object sender, System.ComponentModel.EventArgs e) {
    hdrLabel.Text = ((DayOfWeek)this.GetCurrentColumnValue(this.calculatedField1.Name)).ToString();
}
vb
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
' ...

Private Sub calculatedField1_GetValue(ByVal sender As Object, _ 
ByVal e As GetValueEventArgs) Handles calculatedField1.GetValue
    Dim columnValue As Object = e.GetColumnValue("OrderDate")
    e.Value = CInt(Fix((CDate(columnValue)).DayOfWeek))
End Sub

Private Sub hdrLabel_BeforePrint(ByVal sender As Object, _ 
ByVal e As System.ComponentModel.EventArgs) Handles hdrLabel.BeforePrint
    hdrLabel.Text = (CType(Me.GetCurrentColumnValue(Me.calculatedField1.Name), _ 
    DayOfWeek)).ToString()
End Sub

See Also

Expression

Calculated Fields

Sort Data by Custom Criteria

CalculatedField Class

CalculatedField Members

DevExpress.XtraReports.UI Namespace