)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to calculate formula using ComputeFormulaValueAt method in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: January 3, 2016 |
Last Revised Date: February 13, 2020

Calculate formula using ComputeFormulaValueAt method

By default, the arithmetic operations cannot be parsed in Expression fields. It can be achieved by defined the 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)))

ScreenShot

Show calculate the formula

Samples:

C# : compute Formula At_CS

VB : compute Formula At_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon