Edit this page


RadGridView's MergedCellsStyleSelector can be used to style merged cells differently based on a specific condition.

Bear in mind that the MergedCellsStyle takes precedence over the MergedCellsStyleSelector and will overwrite it if both are defined simultaneously.

To do so, first create a new class that inherits the StyleSelector class and override its SelectStyle method:

Example 1: The StadiumCapacityStyleSelector class

public class StadiumCapacityStyleSelector : StyleSelector
    public override Style SelectStyle(object item, DependencyObject container)
        var cell = item as MergedCellInfo;

        if (cell != null)
            if (int.Parse(cell.Value.ToString()) > 50000)
                return BigStadiumStyle;
                return SmallStadiumStyle;

        return null; 
    public Style BigStadiumStyle { get; set; }
    public Style SmallStadiumStyle { get; set; }
Public Class StadiumCapacityStyleSelector
    Inherits StyleSelector
    Public Overrides Function SelectStyle(item As Object, container As DependencyObject) As Style
        Dim cell = TryCast(item, MergedCellInfo)

        If cell IsNot Nothing Then
            If Integer.Parse(cell.Value.ToString()) > 50000 Then
                Return BigStadiumStyle
                Return SmallStadiumStyle
            End If
        End If

        Return Nothing
    End Function
    Public Property BigStadiumStyle() As Style
            Return m_BigStadiumStyle
        End Get
            m_BigStadiumStyle = Value
        End Set
    End Property
    Private m_BigStadiumStyle As Style
    Public Property SmallStadiumStyle() As Style
            Return m_SmallStadiumStyle
        End Get
            m_SmallStadiumStyle = Value
        End Set
    End Property
    Private m_SmallStadiumStyle As Style
End Class

In the XAML file, define the style selector as a resource and set the properties of the BigStadiumStyle and SmallStadiumStyle:

Example 2: Setting the BigStadiumStyle and SmallStadiumStyle

    <my:StadiumCapacityStyleSelector x:Key="StadiumCapacityStyleSelector">
            <Style TargetType="telerik:GridViewMergedCell">
                <Setter Property="Background" Value="Red"/>
            <Style TargetType="telerik:GridViewMergedCell">
                <Setter Property="Background" Value="Yellow" />

The "my:" prefix before StadiumCapacityStyleSelector specifies the mapping for the namespace of the project: xmlns:my="..."

Finally, set the MergedCellsStyleSelector property:

Example 3: Setting the MergedCellsStyleSelector property

<telerik:RadGridView MergedCellsDirection="Vertical" MergedCellsStyleSelector="{StaticResource StadiumCapacityStyleSelector}" />

And here is the final result:

Figure 1: The merged cells styled using the MergedCellsStyleSelector property

Merged cells styled using the MergedCellsStyleSelector property

See Also

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article