Back to Devexpress

GridView.CustomDrawCell Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-497254c8.md

latest15.8 KB
Original Source

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.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("CustomDraw")]
public event RowCellCustomDrawEventHandler CustomDrawCell
vb
<DXCategory("CustomDraw")>
Public Event CustomDrawCell As RowCellCustomDrawEventHandler

Event Data

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

PropertyDescription
AppearanceGets the painted element’s appearance settings. Inherited from CustomDrawEventArgs.
BoundsReturns a value specifying limits for the drawing area. Inherited from CustomDrawEventArgs.
CacheProvides methods to paint on drawing surfaces in GDI+ and DirectX modes. See DirectX hardware acceleration to learn more. Inherited from CustomDrawEventArgs.
CellProvides information on the painted cell.
CellValueGets the painted value or display text (depending on the event).
ColumnGets the column whose element is being painted.
DisplayTextGets or sets the painted element’s display text.
GraphicsA GDI+ drawing surface. Use the CustomDrawEventArgs.Cache property instead if you enable the DirectX hardware acceleration. Inherited from CustomDrawEventArgs.
HandledGets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
RowHandleGets the handle of a painted element’s row.

The event data class exposes the following methods:

MethodDescription
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.

csharp
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;
    };
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base

CustomDrawCell(gridControl1, gridView1)

Shared Sub CustomDrawCell(ByVal gridControl As GridControl, ByVal gridView As GridView)
    ' Handle this event to paint cells manually
    AddHandler gridView.CustomDrawCell,
        Sub(s, e)
            If e.Column.VisibleIndex <> 2 Then
                Return
            End If
            e.Cache.FillRectangle(Color.Salmon, e.Bounds)
            e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds)
            e.Handled = True
        End Sub
End Sub

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.

csharp
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);
    }
}
vb
Dim warningImage As Image = Image.FromFile("c:\warningImage.png")

Private Sub GridView1_CustomDrawCell(sender As Object, e As RowCellCustomDrawEventArgs) Handles GridView1.CustomDrawCell
    If e.Column.FieldName = "UnitsInStock" Then
        e.DefaultDraw()
        If Convert.ToInt32(e.CellValue) = 0 Then
            e.Cache.DrawImage(warningImage, e.Bounds.Location)
        End If
    End If
End Sub

Example 3

This example paints a thick border around the focused cell.

View Example

csharp
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
    GridView view = sender as GridView;
    if (e.Column == view.FocusedColumn && e.RowHandle == view.FocusedRowHandle) {
        e.DefaultDraw();
        CellDrawHelper.DrawCellBorder(e);
        e.Handled = true;
    }
}
vb
Private Sub gridView1_CustomDrawCell(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    If e.Column = view.FocusedColumn AndAlso e.RowHandle = view.FocusedRowHandle Then
        e.DefaultDraw()
        CellDrawHelper.DrawCellBorder(e)
        e.Handled = True
    End If
End Sub

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.

winforms-grid-move-cell-using-drag-drop/CS/Classes/GridViewHelper.cs#L30

csharp
this.gridView.MouseUp += new MouseEventHandler(gridView_MouseUp);
    this.gridView.CustomDrawCell += new RowCellCustomDrawEventHandler(gridView_CustomDrawCell);
}

winforms-grid-richedit-highlight-search-results/CS/E4422/Form1.cs#L11

csharp
InitializeComponent();
gridView1.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(OnCustomDrawCell);
List<Invoice> invoices = CreateData();

winforms-grid-draw-thick-border-abound-focused-cell/CS/Form1.cs#L82

csharp
this.gridView1.Name = "gridView1";
this.gridView1.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(this.gridView1_CustomDrawCell);
//

winforms-grid-display-colored-progress-bars/CS/ColoredProgressBar/CustomPaintedProgressBarHelper.cs#L13

csharp
_View = _Column.View as GridView;
    _View.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(View_CustomDrawCell);
}

winforms-grid-move-cell-using-drag-drop/VB/Classes/GridViewHelper.vb#L27

vb
AddHandler gridView.MouseUp, AddressOf gridView_MouseUp
    AddHandler gridView.CustomDrawCell, AddressOf gridView_CustomDrawCell
End Sub

winforms-grid-richedit-highlight-search-results/VB/E4422/Form1.vb#L14

vb
InitializeComponent()
AddHandler gridView1.CustomDrawCell, New DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(AddressOf OnCustomDrawCell)
Dim invoices As List(Of Invoice) = CreateData()

winforms-grid-display-icons-in-data-cells/VB/Form1.vb#L156

vb
gridControl1.DataSource = CreateTable()
AddHandler gridView1.CustomDrawCell, AddressOf GridView1_CustomDrawCell
AddHandler gridView1.CustomUnboundColumnData, AddressOf GridView1_CustomUnboundColumnData

winforms-grid-display-colored-progress-bars/VB/ColoredProgressBar/CustomPaintedProgressBarHelper.vb#L17

vb
_View = TryCast(_Column.View, GridView)
    AddHandler _View.CustomDrawCell, New DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(AddressOf View_CustomDrawCell)
End Sub

See Also

Custom Painting Basics

Custom Painting Scenarios

Elements that Can Be Custom Painted

Manually Invalidating Controls

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace