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

GridView.CustomDrawCell Event

Enables data cells to be painted manually.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v18.1.dll

Declaration

[DXCategory("CustomDraw")]
public event RowCellCustomDrawEventHandler CustomDrawCell

Event Data

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

Property Description
Appearance Gets the painted element’s appearance settings. Inherited from CustomDrawEventArgs.
Bounds Returns a value specifying limits for the drawing area. Inherited from CustomDrawEventArgs.
Cache Provides methods to paint on drawing surfaces in GDI+ and DirectX modes. See DirectX hardware acceleration to learn more. Inherited from CustomDrawEventArgs.
Cell Provides information on the painted cell.
CellValue Gets the painted value or display text (depending on the event).
Column Gets the column whose element is being painted.
DisplayText Gets or sets the painted element’s display text.
Graphics A GDI+ drawing surface. Use the CustomDrawEventArgs.Cache property instead if you enable the DirectX hardware acceleration. Inherited from CustomDrawEventArgs.
Handled Gets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
RowHandle Gets the handle of a painted element’s row.

The event data class exposes the following methods:

Method Description
DefaultDraw() Performs default painting of an element. Inherited from CustomDrawEventArgs.

Remarks

The CustomDrawCell event is raised before a data cell is painted. The cell that is about to be painted is identified by the RowCellCustomDrawEventArgs.RowHandle and RowCellCustomDrawEventArgs.Column parameters.

See the Custom Painting Basics and Custom Painting Scenarios topics for information on using custom draw events.

Note

In some cases, a cell’s background may not be repainted by the default painting mechanism (when it is invoked). For instance, if you draw a line, this line may be visible even after the default rendering. However, this is not always true. For instance, if you handle the GridView.RowCellStyle event (in addition to the GridView.CustomDrawCell event) and customize the cell’s background color(s), custom drawing will be cleared and the cell’s background will be repainted by the default painting mechanism after your CustomDrawCell event handler is completed.

We do not recommend that you rely on this behavior, as it may change in the future.

Note

The control’s events designed to change the appearance or rendering of control elements must not be used to update cell values or to modify the control’s layout. Any inappropriate operation which causes a layout update, may break the normal control behavior.

Note

If you subscribe to this event at runtime, your event handler may not be called immediately. The event handler will be raised after the grid’s layout is changed. You can manually invoke the layout change action, and thus your CustomDrawCell event handler, by calling the BaseView.LayoutChanged method after the event subscription.

Example

The following code demonstrates how to use the CustomDrawCell event to re-paint cells that belong to the third Grid Column.

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

CustomDrawCell(gridControl1, gridView1);

public static void CustomDrawCell(GridControl gridControl, GridView gridView) {
    // Handle this event to paint cells manually
    gridView.CustomDrawCell += (s, e) => {
        if (e.Column.VisibleIndex != 2) return;
        e.Cache.FillRectangle(Color.Salmon, e.Bounds);
        e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds);
        e.Handled = true;
    };
}

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