How to calculate formula using ComputeFormulaValueAt method in WinForms GridGroupingControl?
Calculate formula using ComputeFormulaValueAt method
By default,
the arithmetic operations cannot be parsed in Expression fields. It can be
achieved by defined methods manually like SUM,MUL,SUB… and adding those methods
to the library function. So that we can use these methods to perform the
arithmetic operations. By this way, any of the methods can be added for
customization to calculate the values.
C#
ExpressionFieldEvaluator fieldEval;
fieldEval = this.gridGroupingControl1.TableDescriptor.ExpressionFieldEvaluator;
fieldEval.AddFunction("Sum", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Sum));
fieldEval.AddFunction("Sub", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Sub));
fieldEval.AddFunction("Mul", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Mul));
//Method for sum operation
private string Sum(string argument)
{
string[] arg = argument.Split(',');
int result = 0;
foreach (string str in arg)
{
result += Convert.ToInt16(str);
}
return result.ToString();
}
//Expression for compute the formula
MessageBox.Show(fieldEval.ComputeFormulaValueAt(((IExpressionFieldEvaluator)fieldEval).Parse(this.textBox1.Text, true)));
VB
Dim fieldEval as ExpressionFieldEvaluator
fieldEval = Me.gridGroupingControl1.TableDescriptor.ExpressionFieldEvaluator;
fieldEval.AddFunction("Sum", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Sum))
fieldEval.AddFunction("Sub", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Sub))
fieldEval.AddFunction("Mul", new Syncfusion.Grouping.ExpressionFieldEvaluator.LibraryFunction(Mul))
'Method for sum operation
Private Function Sum(ByVal argument As String) As String
Dim arg() As String = argument.Split(","c)
Dim result As Integer = 0
For Each str As String In arg
result += Convert.ToInt16(str)
Next str
Return result.ToString()
End Function
'Expression for compute the formula
MessageBox.Show(fieldEval.ComputeFormulaValueAt((CType(fieldEval, IExpressionFieldEvaluator)).Parse(Me.textBox1.Text, True)))
The ScreenShot below illustrates the computed value
Samples:
Conclusion
I hope you
enjoyed learning how to calculate formula using
ComputeFormulaValueAt method in WinForms GridGroupingControl.
You can refer
to our WinForms
GridGroupingControl’s feature tour page to know about its other
groundbreaking feature representations. You can also explore our WinForms
GridGroupingControl documentation to understand how to present and
manipulate data.
For current
customers, you can check out our WinForms components from the License and Downloads page.
If you are new to Syncfusion, you can try our 30-day free trial to
check out our WinForms GridGroupingControl and
other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!