New to Telerik UI for WPF? Download free 30-day trial

Expression Column

GridViewExpressionColumn derives from the GridViewColumn class and allows you to display various calculations in RadGridView itself. The GridViewExpressionColumn can be sorted and grouped like any other type of column.

For the purposes of this example, we will use the objects and viewmodel defined in examples 1 and 2.

Example 1: The Product class

public class Product 
{ 
    public int ProductID { get; set; } 
    public string ProductName { get; set; } 
    public int UnitPrice { get; set; } 
    public int UnitsInStock { get; set; } 
} 
Public Class Product 
    Public Property ProductID() As Integer 
    Public Property ProductName() As String 
    Public Property UnitPrice() As Integer 
    Public Property UnitsInStock() As Integer 
End Class 

Example 2: The viewmodel

public class MainViewModel : ViewModelBase 
{ 
    private ObservableCollection<Product> products; 
 
    public ObservableCollection<Product> Products 
    { 
        get 
        { 
            if (this.products == null) 
            { 
                this.products = new ObservableCollection<Product>(); 
                for (int i = 1; i <= 5; i++) 
                { 
                    this.products.Add(new Product() { ProductID = i, ProductName = "Product " + i, UnitPrice = i * 10, UnitsInStock = i }); 
                } 
            } 
 
            return this.products; 
        } 
    } 
} 
Public Class MainViewModel 
    Inherits ViewModelBase 
 
    Private products As ObservableCollection(Of Product) 
 
    Public ReadOnly Property Products() As ObservableCollection(Of Product) 
        Get 
            If Me.products Is Nothing Then 
                Me.products = New ObservableCollection(Of Product)() 
                For i As Integer = 1 To 5 
                    Me.products.Add(New Product() With { 
                        .ProductID = i, 
                        .ProductName = "Product " & i, 
                        .UnitPrice = i * 10, 
                        .UnitsInStock = i 
                    }) 
                Next i 
            End If 
 
            Return Me.products 
        End Get 
    End Property 
End Class 

With this data setup, we can bind the GridViewExpressionColumn as shown in Example 3.

Example 3: Define GridViewExpressionColumn in XAML

<telerik:RadGridView x:Name="GridView" ItemsSource="{Binding Products}" AutoGenerateColumns="False" ColumnWidth="*"> 
    <telerik:RadGridView.Columns> 
        <telerik:GridViewDataColumn Header="Product ID" DataMemberBinding="{Binding ProductID}" /> 
        <telerik:GridViewDataColumn Header="Product name" DataMemberBinding="{Binding ProductName}" /> 
        <telerik:GridViewDataColumn Header="Unit price" DataMemberBinding="{Binding UnitPrice}" DataFormatString="{}{0:C}" /> 
        <telerik:GridViewDataColumn Header="Units in stock" DataMemberBinding="{Binding UnitsInStock}" /> 
        <telerik:GridViewExpressionColumn Header="Total value in stock" Expression="UnitPrice * UnitsInStock" DataFormatString="{}{0:C}" /> 
    </telerik:RadGridView.Columns> 
</telerik:RadGridView> 
The most important property to be set is the Expression property. It can also be set in code-behind, for example, if the expression is too complex to be translated in XAML.

Example 4: Set Expression for GridViewExpressionColumn in code.

Expression<Func<Product, double>> expression = prod => prod.UnitPrice * prod.UnitsInStock; 
GridViewExpressionColumn column = this.GridView.Columns.OfType<GridViewExpressionColumn>().First(); 
column.Expression = expression; 
Dim expression As Expression(Of Func(Of Product, Double)) = Function(prod) prod.UnitPrice * prod.UnitsInStock 
Dim column As GridViewExpressionColumn = Me.GridView.Columns.OfType(Of GridViewExpressionColumn)().First() 
column.Expression = expression 

The values displayed in GridViewExpressionColumn will be updated automatically when a concerned property on the data item changes.

More information about expressions could be found here.

In this article