SpreadsheetControl.CustomDrawCellBackground Event
Performs custom painting of the cell background.
Namespace: DevExpress.XtraSpreadsheet
Assembly: DevExpress.XtraSpreadsheet.v20.1.dll
NuGet Package: DevExpress.Win.Spreadsheet
Declaration
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 paints cells containing a formula (determined by the CellRange.HasFormula value) with a hatched brush. Note that the CustomDrawCellEventArgsBase.FillBounds property instead of the CustomDrawCellEventArgsBase.Bounds property is used to get the rectangle to fill.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/xtraspreadsheet-custom-draw-examples-e5044
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
void spreadsheetControl1_CustomDrawCellBackground(object sender, CustomDrawCellBackgroundEventArgs e)
{
if (e.Cell.HasFormula)
{
e.Handled = true;
System.Drawing.Drawing2D.HatchBrush hBrush = new System.Drawing.Drawing2D.HatchBrush(
System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal,
Color.LightGray,
Color.White);
e.Graphics.FillRectangle(hBrush,e.FillBounds);
}
}
Related GitHub Examples
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.