Calculating fields in reports

You can use Report Designer to create fields in your report that do not exist in the data source. This is useful if you want to create a dynamic field that is dependent on other attributes within the data source. To create a dynamic field, you can use a C# expression to work with string, date, or numeric field types.

Steps:
  1. Open a report in the Report Designer.
  2. From the list of Design Elements, click on TextBox and drag the element into position on the report layout.
  3. Enter an expression in the Data Field area in the Element Properties grid. Expressions always begin with an equals ("=") sign.

    Mathematical

    =Quantity + 5

    =Quantity - 3

    =Diameter * 3.14

    =Population / Area

    =(double)System.Math.Round(CostPerUnit*Inventory)

    For more information about the supported math functions see the, MSDN help for the Math Class.

    Conditional

    =(ValueField == 1)?"One":"Not One"

    =(Price < 10)?"Clearance":"Price-Change"

    =(SoilType == System.DBNull.Value) ? "No soil type specified" : SoilType

    Date/Time

    =System.DateTime.Now.ToString()

    =System.DateTime.UtcNow.ToString()

    For more information about the supported date/time functions see the, MSDN help for the DateTime Structure.

    Concatenated

    ="Average Cost: " + avgCostField

    =ZipCode + ZipCode4Digit

    String

    =Name.Substring(0,4)

    =Name.Replace("Test", "Final")

    =Name.ToUpper()

    For more information about the supported string functions see the, MSDN help for the String Class.

  4. Click Run Report to see a preview.
  5. Check the spelling of the field names or the syntax of the expression if the field value returns the error message Control Script failed for Control.

Related Topics


2/11/2011