Skip to main content

SpreadsheetControl.CustomDrawCellBackground Event

Performs custom painting of the cell background.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v24.1.dll

NuGet Package: DevExpress.Win.Spreadsheet

Declaration

public event CustomDrawCellBackgroundEventHandler CustomDrawCellBackground

Event Data

The CustomDrawCellBackground event's data class is CustomDrawCellBackgroundEventArgs. The following properties provide information specific to this event:

Property Description
BackColor Gets or sets the background color of the painted cell.
Bounds Returns the bounding rectangle of the drawing area. Inherited from CustomDrawCellEventArgsBase.
Cache Gets an object that serves as the storage for pens, fonts and brushes. Inherited from CustomDrawObjectEventsArgs.
Cell Gets the worksheet cell being painted. Inherited from CustomDrawCellEventArgsBase.
FillBounds Returns the bounding rectangle of the drawing area for painting the cell background. Inherited from CustomDrawCellEventArgsBase.
Graphics Gets an object used for painting. Inherited from CustomDrawObjectEventsArgs.
Handled Gets or sets whether an event is handled. If true, the default actions are not required. Inherited from CustomDrawObjectEventsArgs.

The event data class exposes the following methods:

Method Description
DrawDefault() Renders the element using the default drawing mechanism. Inherited from CustomDrawObjectEventsArgs.

Remarks

The CustomDrawCellBackground and CustomDrawCell events fire for each cell in the visible worksheet area and allow you to draw cells in a custom manner.

If you perform custom draw actions, set the CustomDrawObjectEventsArgs.Handled property to true to cancel default painting.

You can also combine your custom painting with the default painting. Call the CustomDrawObjectEventsArgs.DrawDefault method to draw cells with the default painter. Set CustomDrawObjectEventsArgs.Handled to true and use the CustomDrawObjectEventsArgs.Graphics property to display custom elements over default graphics.

Important

Never change cell values or format settings on this event. Any action that causes the control’s layout update can lead to an incorrect result or throw an unhandled exception.

Note

In v18.2 and earlier, the CustomDrawCellBackground and CustomDrawCell events fire only for cells that have a value or formatting applied. If you need to restore this behavior, set the EnableLegacyLayoutEngine property to true to switch back to the SpreadsheetControl’s legacy layout engine.

Example

The following code example changes background color for the active cell and cells in odd rows. The Spreadsheet control uses the default drawing mechanism to paint cells with the custom background.

Handle the CustomDrawCellBackground event

View Example: Use Custom Draw Events to Customize the Appearance of Spreadsheet Elements

using System;
using System.Drawing;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
// ...
private void spreadsheetControl1_CustomDrawCellBackground(object sender, 
    CustomDrawCellBackgroundEventArgs e)
{
    // Specify the background color for the active cell. 
    if (e.Cell == spreadsheetControl1.ActiveCell)
    {
        e.BackColor = Color.FromArgb(50, 200, 44, 74);
        return;
    }

    // Specify the background color for cells that contain data and belong to odd rows.
    var dataRange = e.Cell.Worksheet.GetDataRange();
    if (e.Cell.IsIntersecting(dataRange) && e.Cell.RowIndex % 2 != 0) 
    {             
        e.BackColor = Color.FromArgb(50, 91, 155, 213);
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomDrawCellBackground event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also