xtrareports-devexpress-dot-xtrareports-dot-expressions.md
Contains static methods to register a custom function for the Expression Editor in the Report Designer.
Namespace : DevExpress.XtraReports.Expressions
Assembly : DevExpress.XtraReports.v25.2.dll
NuGet Package : DevExpress.Reporting.Core
public class CustomFunctions :
BaseCustomFunctions
Public Class CustomFunctions
Inherits BaseCustomFunctions
The CustomFunctions class allows you to register a custom function that users can utilize in the Expression Editor for expression bindings and calculated fields.
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;
namespace CustomFunctionForExpressionEditorExample
{
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);
}
}
}
#Region "#usings"
Imports DevExpress.XtraReports.Expressions
Imports System
#End Region
Namespace CustomFunctionForExpressionEditorExample
#Region "#CustomFormatFunction"
Public Class CustomFormatFunction
Inherits ReportCustomFunctionOperatorBase
Public Overrides ReadOnly Property FunctionCategory() As String
Get
Return "String"
End Get
End Property
Public Overrides ReadOnly Property Description() As String
Get
Return "CustomFormatFunction(string format, object arg0)" _
& ControlChars.CrLf & "Converts an arg0 value to a string based on a specified format"
End Get
End Property
Public Overrides Function IsValidOperandCount(ByVal count As Integer) As Boolean
Return count = 2
End Function
Public Overrides Function IsValidOperandType(ByVal operandIndex As Integer,
ByVal operandCount As Integer, ByVal type As Type) As Boolean
Return True
End Function
Public Overrides ReadOnly Property MaxOperandCount() As Integer
Get
Return 2
End Get
End Property
Public Overrides ReadOnly Property MinOperandCount() As Integer
Get
Return 2
End Get
End Property
Public Overrides Function Evaluate(ParamArray ByVal operands() As Object) As Object
Dim res As String = String.Format(operands(0).ToString(), operands(1))
Return res
End Function
Public Overrides ReadOnly Property Name() As String
Get
Return "CustomFormatFunction"
End Get
End Property
Public Overrides Function ResultType(ParamArray ByVal operands() As Type) As Type
Return GetType(String)
End Function
End Class
#End Region
End Namespace
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:
{
// ...
DevExpress.XtraReports.Expressions.CustomFunctions.Register(new CustomFormatFunction());
// ...
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' ...
DevExpress.XtraReports.Expressions.CustomFunctions.Register(New CustomFormatFunction())
' ...
End Sub
The Expression Editor displays the CustomFormatFunction in the String category:
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.
Do the following to register a function for use in the Expression Editor for the Visual Studio Report Designer:
Create a Component class in Visual Studio.
Register a custom function in the component’s constructor:
Rebuild the project.
Open a report in the Visual Studio Report Designer.
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.
Review the following help topics:
Object BaseCustomFunctions CustomFunctions
See Also