INFO: Using DataField Expressions in Calculated Fields


06-22-2007, 4:07 PM
ActiveReports for .NET supports the use of expressions in the DataField property. Ordinarily, the DataField property is set to a single value, which corresponds to an individual Field name in the Fields collection. Often, to add another Field to a report, developers can add a column to an SQL query or to the data source. They can also programmatically add a custom Field to the Fields collections in the DataInitialize event and then set its value in the FetchData event. However, if the desired result can be expressed in terms of one or more existing Field values or other conditions (such as the current time), then developers can also use an expression to calculate a value. This is often referred to as a calculated field and is specified as a value for the DataField property.

An expression for a calculated field must start with an equal "=" character. This instructs the reporting engine that the value is an expression and not just a field value. The expression following the equal sign can be a single line of C# code. This code can reference current values in the Fields collection as variables, a public member of the report's class, or any of the following .NET namespaces.

References to additional .NET assemblies may be added in future versions.

The following are some examples of valid expressions. The semicolon (;) at the end of the line is optional. These can be assigned by setting the DataField property of any bound control at design time, or by setting the DataField property in code, either before the report runs or in the ReportStart event.

Current date and time
= System.DateTime.Now.ToString()
= System.DateTime.Now.ToString();

Date only
= System.DateTime.Now.ToShortDateString()
= System.DateTime.Now.ToLongDateString()

Time only
= System.DateTime.Now.ToShortTimeString()
= System.DateTime.Now.ToLongTimeString()

Day of week (name)
= System.DateTime.Now.DayOfWeek

Day of week (number)
= (int)System.DateTime.Now.DayOfWeek

Five days from now
= System.DateTime.Now.AddDays(5).ToLongDateString()

10% markup price
= UnitPrice + UnitPrice*0.1

10% markup price as currency (.NET 2.0)
= (UnitPrice + UnitPrice*0.1).ToString("C")

If unit price is more than 15 and the product is in category 3, display that it is eligible for a discount, but otherwise display nothing.
= (UnitPrice > 15) && (CategoryID == 3) ? "Eligible for discount" : ""
 
Note that you must fully qualify the class name by including the namespace. For example, the following expression will work.

= System.DateTime.Now.ToShortTimeString()

However, the next expression will not work. Note that it is missing the "System" namespace.

= DateTime.Now.ToShortTimeString()

In addition, for more complicated calculations or tasks, you can also call a public method of the report class. (It is important to remember, that in ActiveReports for .NET, you have access to the class file of the actual report. Since you can modify it, this can be used to provide enormous flexibility in your reporting logic.) To do this, simply add a public method to your report, and access it in the DataField by using the "this" pointer.

The following steps summarize the process of adding a public method to a report and calling it in a DataField expression.

Step 1 - Add a public method

[VB.NET]

Public Class rptProducts
    Inherits DataDynamics.ActiveReports.ActiveReport3

    'other code omitted for readability

    Public Function IsTimeToReorderProduct(ByVal UnitsInStock As Integer, ByVal ReorderLevel As Integer) As String
        If UnitsInStock <= ReorderLevel Then
            Return "Yes"
        Else
            Return "No"
        End If
    End Function
End Class


[C#]

public class rptProducts : DataDynamics.ActiveReports.ActiveReport3
{

    //other code omitted for readability

    public string IsTimeToReorderProduct(int UnitsInStock, int ReorderLevel)
    {
        if (UnitsInStock <= ReorderLevel)
            return "Yes";
        else
            return "No";
    }
}


Step 2 - Set the DataField Property of a Control to an Expression

= this.IsTimeToReorderProduct(UnitsInStock, ReorderLevel);

The attached sample project contains a working example of this code for both VB.NET and C#. Be sure to adjust the path to the Northwinds sample database as appropriate for your system. By default the sample report looks for the database in C:\NWIND.MDB.