Computing Values in the RadGridView for WinForms

Thursday, May 14, 2009 by WinForms Team | Comments 2

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

2 Comments

  • Pratik Kothari 14 May 2009
    we have a radgridview in winforms in which we are presenting hierarchical data.
    So, parent1 may have 10 childrows
    parent2 has 15 childrows for example.

    Is there a way, the parent rows can be sorted based on the count of the number of child rows they have?
  • John Kellar 15 May 2009

    Hi Pratik,

        Idon’t think you could easily accomplish the task using the Expression featurebecause it would not be aware of the grouping.If you were to account for the grouping in your expression maybe but Ithink the end result would be some rather convoluted code that you would notwant to maintain.  I recommend you postthis question in the Telerik forums to get a second opinion though, there isalways a possibility I am missing something.  You might want to look at the AggregateFunctions feature of the grid, which can be applied to groups as well.  I know this isn’t a great answer, but I hopeyou find it useful.

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)