Articles in this section
Category / Section

How can I resolve the exception occurred when division operation is used in ExpressionDescriptor?

2 mins read

Problem:

A decimal does not support unknown division operation, that is, any number divided by zero or zero divided by any number.

Reason:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The decimal number can be used for calculations under following criteria:

  • A Decimal is a 128-bit floating point data type,that should be used where precision is of extreme importance, such as monetary calculations. For more details, please refer this link: what-is-the-difference-between-decimal-float-and-double-in-net
  • One reason that NaN and Infinity values make more sense for floating point arithmetic is that the operations could result in division by zero, not only because the divisor is actually zero, but because it is too small to be represented by the type. For more details, please refer this link: why-is-nan-not-a-number-only-available-for-doubles

Solution:

To perform the division operation in the Expression Field, you can use Double, Integer type. Decimal return type does not support the NaN, Infinity results, because of the above criteria. It can be used only for exact numbers. When you want to perform the division operations while updating the data in the grid, you can use the Double or Integer types.

For example, assume you have a grid with expression columns, Column [A], Column [B] and Column [C]. The Expression column is calculated depending on the values of Column [A] and Column [B]. Exception is thrown when you add the data to the grid for one column, assuming it to be Column [A], it automatically calculates column [B] as zero. Then expression column automatically calculates division operation for Column [A] / 0, that is, column [B], so the result is Infinity.

C#

ExpressionFieldDescriptor adc = new ExpressionFieldDescriptor("Total", "[A]/[B]", typeof(double));
gridGroupingControl1.TableDescriptor.ExpressionFields.Add(adc);

VB

Dim adc As ExpressionFieldDescriptor = New ExpressionFieldDescriptor("Total", "[A]/[B]", GetType(System.Double))gridGroupingControl1.TableDescriptor.ExpressionFields.Add(adc)

Sample:

http://www.syncfusion.com/downloads/support/directtrac/general/Expression_Column_Result_Type_As_Double-43158780.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied