Edit this page

Formatting Data Cells

The CellFormatting event is used to add formatting to grid cells. For example, the code sample below changes the ForeColor, BackColor and GradientStyle in all data cells in the ContactTitle column if the cell value is Owner:


private void radVirtualGrid1_CellFormatting(object sender, VirtualGridCellElementEventArgs e)
    if (e.CellElement.RowIndex > -1)
        if (e.CellElement.ColumnIndex == 3 && e.CellElement.Value != null && e.CellElement.Value.ToString() == "Owner")
            e.CellElement.DrawFill = true;
            e.CellElement.BackColor = Color.Yellow;
            e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
            e.CellElement.ForeColor = Color.Red;
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);

Private Sub radVirtualGrid1_CellFormatting(sender As Object, e As VirtualGridCellElementEventArgs)
    If e.CellElement.RowIndex > -1 Then
        If e.CellElement.ColumnIndex = 3 AndAlso e.CellElement.Value IsNot Nothing _
        AndAlso e.CellElement.Value.ToString() = "Owner" Then
            e.CellElement.DrawFill = True
            e.CellElement.BackColor = Color.Yellow
            e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid
            e.CellElement.ForeColor = Color.Red
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local)
        End If
    End If
End Sub


Due to the UI virtualization in RadVirtualGrid, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, sorting and so on. In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse) all customization should be reset for the rest of the cell elements.

See Also