GridView.CustomDrawCell Event
Fires for all currently visible data cells and allows you to manually draw them. Note that modifications you apply on this event are ignored when you print or export Grid data.
Namespace: DevExpress.XtraGrid.Views.Grid
Assembly: DevExpress.XtraGrid.v24.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
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. |
DrawHtml(HtmlTemplate, DxHtmlPainterContext, Action<DxHtmlPainterArgs>) | Paints the required HTML template inside an element that raised this event. The context parameter allows you to assign an object that transfers mouse events to template elements. Inherited from CustomDrawEventArgs. |
DrawHtml(HtmlTemplate, Action<DxHtmlPainterArgs>) | Paints the required HTML template inside an element that raised this event. Inherited from CustomDrawEventArgs. |
Remarks
The CustomDrawCell event is raised before a data cell is painted. To identify the currently processed cell, read the event’s RowCellCustomDrawEventArgs.RowHandle and RowCellCustomDrawEventArgs.Column parameters.
If you subscribe to this event at runtime, your event handler is raised next time the Grid layout changes. You can call BaseView.LayoutChanged method to trigger the event.
Once you have performed required custom draw actions, set the e.Handled parameter to true to prevent the default renderer from changing this cell. Otherwise, your custom appearance can be lost.
You can draw cells with the default painter (call the CustomDrawEventArgs.DefaultDraw method to do that), and then add custom-painted elements on top of these cells. See “Example 2” in this article for a sample code.
Refer to the Custom Painting Basics and Custom Painting Scenarios articles for more information.
Note that the CustomDrawCell
event is ignored when printing\exporting Grid data. Refer to the A1498 - Is custom drawing ignored when printing or exporting? KB article to learn how to keep your customizations.
Important
Do not change cell values, modify the control’s layout, or change the control’s object model in the events used for custom control painting. Actions that update the layout can cause the control to malfunction.
Note
If you subscribe to this event at runtime, your event handler will be raised next time the Grid layout changes. You can call BaseView.LayoutChanged method to manually trigger the event.
Example 1
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;
};
}
Example 2 - Default Draw
In the sample below, the GridView.CustomDrawCell
event is handled to custom-paint data cells.
The CustomDrawEventArgs.DefaultDraw method applies the default draw to all cells. For all “Units in Stock” cells that display 0, a custom icon is drawn on top of this default cell rendering.
Image warningImage = Image.FromFile("c:\\warning.png");
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
if (e.Column.FieldName == "UnitsInStock") {
e.DefaultDraw();
if (Convert.ToInt32(e.CellValue) == 0)
e.Cache.DrawImage(warningImage, e.Bounds.Location);
}
}
Related GitHub Examples
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.