When working with data there are a number of occasions where you may want to calculate a value for display within a grid. Did you know you can do this within the RadGridView itself? It is really quite simple.
To compute a column within the RadGridView you need to set the Expression property of the destination column. This may be a new column that you are adding to the grid to store the value or you can override the value of an existing column by setting the Expression property. Below is an example of the syntax to calculate an average across all the rows in the Quantity column of the RadGridView.
Note that the Quantity column is used as a keyword within the expression and does not expect quotes. Since we are setting Column 0’s Expression property to the calculation, the result will be displayed in Column 0 on every row. While this may demonstrate that you can perform an operation across all the rows, I think the next example is a little better.
In the code above, we calculate the average of quantity across the entire grid and then subtract the current row’s Quantity value from the average to indicate whether the current record is above or below the average. You might have noticed the use of the minus (-) symbol within the expression. There are a number of available operators that can be used within an expression.
Arithmetic Operators: Add (+), Subtract (-), Multiply (*), Divide (/) Boolean Operators: AND, OR, NOT Logical Operators: Equals (=), LessThan (<), GreaterThan (>), LessThanOrEqual (<=), GreaterThanOrEqual (>=) Complex Operators: IN, LIKE, Modulus (%)
With the operators available, you have the ability to build very complex expressions to calculate values within your grid.
The code above demonstrates the use of a date within an expression. All dates must be contained within the pound (#) to identify it as a date. When dealing with string comparisons, the value should be contained within single quotes (‘), see the example below.
The example shows the use of the LIKE operator and the wildcard (%) operator. The result of this expression would be any person, whose first name started with a J would have a value of 1 (true) and 0 (false), see below. You may also use the alternate wildcard operator (*) within a string value. The wildcard operators are limited to being used at either the beginning or end of the string being compared.
Nikolay Diyanov Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.
Find him on Twitter @n_diyanov or on LinkedIn.
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.