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.

radGridView1.Columns[0].Expression = "AVG(Quantity)"
 

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.

radGridView1.Columns[0].Expression = "AVG(Quantity) - Quantity"
 

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.

radGridView1.Columns[0].Expression = "OrderDate >= #01/01/1997#"
 

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.

radGridView1.Columns[0].Expression = "FirstName LIKE ‘J%’"
 

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. 

Screenshot


About the Author

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.

Related Posts

Comments

Comments are disabled in preview mode.