Skip to main content
A newer version of this page is available. .

SpreadsheetControl.CustomDrawCell Event

Allows you to paint cell content.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v19.2.dll

Declaration

public event CustomDrawCellEventHandler CustomDrawCell

Event Data

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

Property Description
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.
Font Gets the font attributes of the painted cell.
ForeColor Gets or sets the color of the text within the painted cell.
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.
Text Gets or sets the painted cell’s text.

The event data class exposes the following methods:

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

Remarks

The CustomDrawCell and CustomDrawCellBackground 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 CustomDrawCell and CustomDrawCellBackground 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 demonstrates how to handle the CustomDrawCell event. The built-in worksheet function RANK.AVG (listed in Statistical Functions document) is used to calculate the rank of the current cell value in the row. The IWorkbook.Evaluate method provides access to worksheet functions, performs the calculation and returns the result. The rank value is transformed into a Roman numeral using the INumberInWordsProvider.ConvertToText method. The Graphics object is used to draw a Roman numeral in the left upper corner of the cell.

Only the top three ranks are displayed. Certain rows are excluded, i.e., custom draw is not performed for cells in those rows.

An area of a sample worksheet that displays cell ranks is shown in the image below.

CustomDrawCell

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
        void spreadsheetControl1_CustomDrawCell(object sender, CustomDrawCellEventArgs e)
        {
            if (e.Cell.RowIndex == 0 || e.Cell.RowIndex >= 3) {
                Font headingFont = new Font("Times New Roman", e.Font.Size);
                string cellRef = e.Cell.GetReferenceR1C1(ReferenceElement.RowAbsolute | ReferenceElement.ColumnAbsolute, null);
                string formula = String.Format("=RANK.AVG({0},R{1}C{2}:R{3}C{4})", cellRef, e.Cell.RowIndex + 1, 2, e.Cell.RowIndex + 1, 10);
                int rank = (int)spreadsheetControl1.Document.Evaluate(formula).NumericValue;
                // The DevExpress.Docs.Text.NumberInWords class requires a reference to the DevExpress.Docs assembly. 
                // To redistribute the DevExpress.Docs assembly the DevExpress Universal subscription or the Document Server license is required.
                string rankText = DevExpress.Docs.Text.NumberInWords.Cardinal.ConvertToText(rank, DevExpress.Docs.Text.NumberCulture.Roman);
                if (rank > 0 && rank < 4) e.Graphics.DrawString(rankText, headingFont, e.Cache.GetSolidBrush(Color.Red), e.Bounds.Left, e.Bounds.Top);
            }
        }

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomDrawCell 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