Tutorial: Custom Styles for Rows and Cells

  • 2 minutes to read

This walkthrough is a transcript of the Custom Styles for Rows and Cells video available on the DevExpress YouTube Channel.

In this tutorial, you'll learn how to change data cell styles using events. You'll start with a grid displaying task data and having no conditional formatting applied. By handling the GridView.RowStyle event, you'll apply a different background color to rows whose Priority field value is High. Then, you'll use the GridView.RowCellStyle event to highlight Status cells within those rows, if the Status is set to New.

GridView_Appearance_RowAndCellStylesResult

Starting Point

Start with an application that has a grid that displays task data and has no conditional formatting applied. Run the application to see how the data looks.

GridView_Appearance_RowAndCellStylesInitialGrid

Customizing the Appearance of Individual Rows

Write the GridView.RowStyle event handler. The code changes the background color for rows whose Priority column value is High. The currently processed row is identified using the event's RowEventArgs.RowHandle parameter. The row's appearance settings are set using the RowStyleEventArgs.Appearance parameter.


private void gridView_RowStyle(object sender, RowStyleEventArgs e) {
    GridView View = sender as GridView;
    if (e.RowHandle >= 0) {
        string priority = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Priority"]);
        if (priority == "High") {
            e.Appearance.BackColor = Color.FromArgb(150, Color.LightCoral);
            e.Appearance.BackColor2 = Color.White;
        }
    }
}

Run the application and see the result. The "high priority" rows are now displayed with the specified gradient background. The gradient is applied to the entire row and not to individual cells.

GridView_Appearance_RowStyleResult

Customizing the Appearance of Individual Cells

Return to design time and customize individual cells. For this purpose, handle the GridView.RowCellStyle event. It provides you with the CustomRowCellEventArgs.Column and CustomRowCellEventArgs.RowHandle parameters that identify the cell being processed. The handler will modify cells in the Status column if their value is New, provided that the record's Priority is set to High.


private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e) {
    GridView View = sender as GridView;
    if (e.Column.FieldName == "Status") {
        string status = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Status"]);
        string priority = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Priority"]);
        if (status == "New" && priority == "High") {
            e.Appearance.BackColor = Color.FromArgb(150, Color.Salmon);
            e.Appearance.BackColor2 = Color.FromArgb(150, Color.Salmon);
        }
    }
}

Run the application. You'll see that appearance settings imposed by the GridView.RowCellStyle event override those set in the GridView.RowStyle event.

GridView_Appearance_RowAndCellStylesResult

See Also