Back to Devexpress

SpreadsheetControl.CustomDrawCellBackground Event

windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-96956340.md

latest8.2 KB
Original Source

SpreadsheetControl.CustomDrawCellBackground Event

Performs custom painting of the cell background.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.XtraSpreadsheet.v25.2.dll

NuGet Package : DevExpress.Win.Spreadsheet

Declaration

csharp
public event CustomDrawCellBackgroundEventHandler CustomDrawCellBackground
vb
Public Event CustomDrawCellBackground As CustomDrawCellBackgroundEventHandler

Event Data

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

PropertyDescription
BackColorGets or sets the background color of the painted cell.
BoundsReturns the bounding rectangle of the drawing area. Inherited from CustomDrawCellEventArgsBase.
CacheGets an object that serves as the storage for pens, fonts and brushes. Inherited from CustomDrawObjectEventsArgs.
CellGets the worksheet cell being painted. Inherited from CustomDrawCellEventArgsBase.
FillBoundsReturns the bounding rectangle of the drawing area for painting the cell background. Inherited from CustomDrawCellEventArgsBase.
GraphicsGets an object used for painting. Inherited from CustomDrawObjectEventsArgs.
HandledGets 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:

MethodDescription
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.

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

csharp
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);
    }
}
vb
Imports System
Imports System.Drawing
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet
' ...
Private Sub spreadsheetControl1_CustomDrawCellBackground(ByVal sender As Object,
    ByVal e As CustomDrawCellBackgroundEventArgs)
    ' Specify the background color for the active cell. 
    If e.Cell Is spreadsheetControl1.ActiveCell Then
        e.BackColor = Color.FromArgb(50, 200, 44, 74)
        Return
    End If

    ' Specify the background color for cells that contain data and belong to odd rows.
    Dim dataRange = e.Cell.Worksheet.GetDataRange()
    If e.Cell.IsIntersecting(dataRange) AndAlso e.Cell.RowIndex Mod 2 <> 0 Then
        e.BackColor = Color.FromArgb(50, 91, 155, 213)
    End If
End Sub

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.

winforms-spreadsheet-custom-draw-example/VB/CustomDrawExample/Form1.vb#L24

vb
AddHandler spreadsheetControl1.CustomDrawCell, AddressOf spreadsheetControl1_CustomDrawCell
    AddHandler spreadsheetControl1.CustomDrawCellBackground, AddressOf spreadsheetControl1_CustomDrawCellBackground
End Sub

See Also

CustomDrawCell

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.XtraSpreadsheet Namespace