Articles in this section
Category / Section

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 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;

//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;

'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

Samples:

Did you find this information helpful?
Yes
No