How to Add Custom Calculations to Expression Fields
27 Mar 2018 / 3 minutes to read
Essential Grouping lets you add your own functions to a function library which can be used in an expression field. In this manner, you can do custom calculations in expressions. This is a two-step process which is given below:
- Register the function name and a delegate with the grouping engine.
- Implement a method that does your calculation.
In the code given below, we add a method named ‘New_Function’ that takes two arguments and performs a certain calculation on those arguments.
// Step 1
// Add function named New_Function that uses a delegate named ComputeFunction to define a custom calculation.
ExpressionFieldEvaluator evaluator = this.groupingEngine.TableDescriptor.ExpressionFieldEvaluator;
evaluator.AddFunction("New_Function", new ExpressionFieldEvaluator.LibraryFunction(ComputeFunction));
// Sample usage in an Expression column.
this.groupingEngine.TableDescriptor.ExpressionFields.Add("test");
this.groupingEngine.TableDescriptor.ExpressionFields["test"].Expression = "New_Function([Col1], [Col2])";
//...
// Step 2
// Define ComputeFunction that returns the absolute value of the 1st arg minus 2 * the 2nd arg.
public string ComputeFunction(string s)
{
// Get the list delimiter (for en-us, it is a comma).
char comma = Convert.ToChar(this.gridGroupingControl1.Culture.TextInfo.ListSeparator);
string[] ss = s.Split(comma);
if(ss.GetLength(0) != 2)
throw new ArgumentException("Requires 2 arguments.");
double arg1, arg2;
if(double.TryParse(ss[0], System.Globalization.NumberStyles.Any, null, out arg1)
&& double.TryParse(ss[1], System.Globalization.NumberStyles.Any, null, out arg2))
{
return Math.Abs(arg1 - 2 * arg2).ToString();
}
return "";
}
' Step 1
' Add function named New_Function that uses a delegate named ComputeFunction to define a custom calculation.
Dim evaluator As ExpressionFieldEvaluator = Me.groupingEngine.TableDescriptor.ExpressionFieldEvaluator
evaluator.AddFunction("New_Function", New ExpressionFieldEvaluator.LibraryFunction(ComputeFunction))
' Sample usage in an Expression column.
Me.groupingEngine.TableDescriptor.ExpressionFields.Add("test")
Me.groupingEngine.TableDescriptor.ExpressionFields("test").Expression = "New_Function([Col1], [Col2])"
' Step 2
' Define ComputeFunction that returns the absolute value of the 1st arg minus 2 * the 2nd arg.
Public Function ComputeFunction(ByVal s As String) As String
' Get the list delimiter (for en-us, it is a comma).
Dim comma As Char = Convert.ToChar(Me.gridGroupingControl1.Culture.TextInfo.ListSeparator)
Dim ss As String() = s.Split(comma)
If ss.GetLength(0) <> 2 Then
Throw New ArgumentException("Requires 2 arguments.")
End If
Dim arg1, arg2 As Double
If Double.TryParse(ss(0), System.Globalization.NumberStyles.Any, Nothing, arg1) AndAlso _
Double.TryParse(ss(1), System.Globalization.NumberStyles.Any, Nothing, arg2) Then
Return Math.Abs((arg1 - 2 * arg2)).ToString()
End If
Return ""
' ComputeFunction
End Function
Was this page helpful?
Yes
No
Thank you for your feedback!
Thank you for your feedback and comments. We will rectify this as soon as possible!
An unknown error has occurred. Please try again.
Help us improve this page