CustomFunctions Class

Contains static methods to register a custom function for the Expression Editor in the Report Designer.

Namespace: DevExpress.XtraReports.Expressions

Assembly: DevExpress.XtraReports.v21.1.dll

Declaration

public static class CustomFunctions

Remarks

The CustomFunctions class allows you to register a custom function that users can utilize in the Expression Editor for expression bindings and calculated fields.

Create a Custom Function (WinForms Report Designer)

To create a custom function, create an object that descends from the ReportCustomFunctionOperatorBase abstract class.

The following code defines a custom function in the String category:

using DevExpress.XtraReports.Expressions;
using System;
// ...
    public class CustomFormatFunction : ReportCustomFunctionOperatorBase
    {
        public override string FunctionCategory 
            => "String";
        public override string Description 
            => "CustomFormatFunction(string format, object arg0)" +
            "\r\nConverts an arg0 value to a string based on a specified format";
        public override bool IsValidOperandCount(int count) 
            => count == 2;
        public override bool IsValidOperandType(int operandIndex, int operandCount, Type type) 
            => true;
        public override int MaxOperandCount
            => 2;
        public override int MinOperandCount
            => 2;
        public override object Evaluate(params object[] operands)
        {
            string res = String.Format(operands[0].ToString(), operands[1]);
            return res;
        }
        public override string Name 
            => "CustomFormatFunction";
        public override Type ResultType(params Type[] operands)
        {
            return typeof(string);
        }
    }

Call the static CustomFunctions.Register method to register a custom function. Pass the custom function class instance as a parameter. To unregister a custom function, use the static CustomFunctions.Unregister method.

The following code registers a custom function for use in the End-User Designer:

private void Form1_Load(object sender, EventArgs e)
{
// ...
    DevExpress.XtraReports.Expressions.CustomFunctions.Register(new CustomFormatFunction());
    // ...
    reportDesigner1.OpenReport(new Reports.XtraReport1());
}

The Expression Editor displays the CustomFormatFunction in the String category:

Expression Editor Custom Function

Custom functions registered in the Form constructor are available only in the Report Designer on the form. They are not available in the Visual Studio Report Designer. You can proceed without the Expression Editor and use the Property Grid editor to type a custom function in an expression.

Register a Custom Function in the Visual Studio Report Designer

Do the following to register a function for use in the Expression Editor for the Visual Studio Report Designer:

  1. Create a Component class in Visual Studio.
  2. Register a custom function in the component’s constructor:

    public partial class Component1 : Component
    {
        public Component1()
        {
            InitializeComponent();
            DevExpress.XtraReports.Expressions.CustomFunctions.Register(new NewLineConstant());
            DevExpress.XtraReports.Expressions.CustomFunctions.Register(new CustomFormatFunction());
        }
    
        public Component1(IContainer container)
        {
            container.Add(this);
    
            InitializeComponent();
        }
    }
    
  3. Rebuild the project.

  4. Open a report in the Visual Studio Report Designer.
  5. Drag Component1 from the Toolbox and drop it on the report’s design surface.

Custom functions are now available for use in expressions and in the Expression Editor.

View Example: Expression Editor - How to Implement a Custom Function in a Reporting Application for ASP.NET Core

Create a Custom Function (Web)

Review the following help topics:

Inheritance

Object
CustomFunctions
See Also