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

SpreadsheetControl.CustomDrawCell Event

Provides the capability to perform custom painting of cell content.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v18.1.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.
Cache Gets an object which specifies the storage for the pens, fonts and brushes. Inherited from CustomDrawObjectEventsArgs.
Cell Gets the worksheet cell being painted.
FillBounds Returns the bounding rectangle of the drawing area for painting the cell background.
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 event is raised before a worksheet Cell is painted. Event arguments properties provide the objects and information required to paint the cell.

Set the CustomDrawObjectEventsArgs.Handled property to true to cancel default painting. Use the CustomDrawObjectEventsArgs.DrawDefault method to perform default painting within the event handler.

The CustomDrawCell event is fired only for existing cells, i.e., the cells that have formatting or content different from the default. Use the Worksheet.GetUsedRange method to obtain a range that comprises existing cells.

Important

Do not change cell values or formatting within this event handler. The events designed to change the appearance or render control elements must not be used to update cell values or to modify the control’s layout. Any operation which causes a layout update may result in incorrect operation or may throw an unhandled exception.

Example

The following code uses the built-in worksheet function RANK.AVG (listed in Statistical Functions document) to calculate the rank of the current cell value in the row. The IWorkbook.Evaluate method of a workbook 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