GrapeCity MultiRow Windows Forms Documentation
User Defined Conditional Styles

You can apply cell styles based on user defined conditions if you use dynamic cell styles (DynamicCellStyle class). Since the conditions for dynamic cell styles are set with code, they are not available in the designer.

Using Code

The following code changes the cell backcolor for alternating rows.

[VB]

Imports GrapeCity.Win.MultiRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim dynamicCellStyle1 As New DynamicCellStyle()
    dynamicCellStyle1.ConditionHandler = New DynamicCellStyleConditionHandler(AddressOf MyCondition)

    Dim template1 As Template = Template.Default
    template1.Row.DefaultCellStyle = dynamicCellStyle1
    GcMultiRow1.Template = template1
    GcMultiRow1.RowCount = 3
End Sub

Private Function MyCondition(ByVal context As DynamicCellStyleContext) As CellStyle
    Dim cellStyle1 As New CellStyle
    ' Change the Back Color of alternating row
    If context.CellScope = CellScope.Row AndAlso (context.RowIndex Mod 2) = 0 Then
        cellStyle1.BackColor = Color.LightCyan
    Else
        cellStyle1.BackColor = Color.LightSalmon
    End If
    Return cellStyle1
End Function

[CS]

using GrapeCity.Win.MultiRow;

private void Form1_Load(object sender, EventArgs e)
{
    DynamicCellStyle dynamicCellStyle1 = new DynamicCellStyle();
    dynamicCellStyle1.ConditionHandler = new DynamicCellStyleConditionHandler(MyCondition);

    Template template1 = Template.Default;
    template1.Row.DefaultCellStyle = dynamicCellStyle1;
    gcMultiRow1.Template = template1;
    gcMultiRow1.RowCount = 3;
}

private CellStyle MyCondition(DynamicCellStyleContext context)
{
    CellStyle cellStyle1 = new CellStyle();
    // Change the Back Color of alternating row
    if (context.CellScope == CellScope.Row && (context.RowIndex % 2) == 0)
    {
        cellStyle1.BackColor = Color.LightCyan;
    }
    else
    {
        cellStyle1.BackColor = Color.LightSalmon;
    }
    return cellStyle1;
}

The following image shows the result of the code at runtime:

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options