Edit this page

Formatting Rows

Customize the appearance of data rows

Use the RowFormatting event to apply custom formatting to RadGridView's data rows.

The code snippet below demonstrates changing the background color of rows, which "BMP" cell value is set to true:

private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if ((bool)e.RowElement.RowInfo.Cells["BMP"].Value == true)
    {
        e.RowElement.DrawFill = true;
        e.RowElement.GradientStyle = GradientStyles.Solid;
        e.RowElement.BackColor = Color.Aqua;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

Private Sub RadGridView1_RowFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles RadGridView1.RowFormatting
    If e.RowElement.RowInfo.Cells("BMP").Value = True Then
        e.RowElement.DrawFill = True
        e.RowElement.GradientStyle = GradientStyles.Solid
        e.RowElement.BackColor = Color.Aqua
    Else
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
    End If
End Sub

note

An if-else statement is used to reset the value of BackColorProperty if no drawing is required.

note

You should set DrawFill to true to turn on the fill for the row (depends on the used theme).

note

Please refer to the Fundamentals topic for more information about the UI Virtualization.

Customize the non-data rows appearance

To customize the non-data rows (header row, new row, filtering row, etc) of RadGridView, you need to handle the ViewRowFormatting event.

void radGridView1_ViewRowFormatting(object sender, RowFormattingEventArgs e)
{

    if (e.RowElement is GridTableHeaderRowElement)
    {
        e.RowElement.DrawFill = true;
        e.RowElement.BackColor = Color.Navy;
        e.RowElement.NumberOfColors = 1;
        e.RowElement.ForeColor = Color.White;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
    }
}

Private Sub RadGridView1_ViewRowFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles RadGridView1.ViewRowFormatting
    If TypeOf e.RowElement Is GridTableHeaderRowElement Then
        e.RowElement.DrawFill = True
        e.RowElement.BackColor = Color.Navy
        e.RowElement.NumberOfColors = 1
        e.RowElement.ForeColor = Color.White
    Else
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local)
    End If
End Sub

grid-rows-formatting-rows 002

See Also