Back to Devexpress

GridView.CustomDrawColumnHeader Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-0fd6bf39.md

latest13.4 KB
Original Source

GridView.CustomDrawColumnHeader Event

Enables you to paint column headers manually.

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 ColumnHeaderCustomDrawEventHandler CustomDrawColumnHeader
vb
<DXCategory("CustomDraw")>
Public Event CustomDrawColumnHeader As ColumnHeaderCustomDrawEventHandler

Event Data

The CustomDrawColumnHeader event's data class is ColumnHeaderCustomDrawEventArgs. 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.
ColumnGets the GridColumn whose header is to be drawn. Returns null if an “empty column header” is currently being painted.
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.
InfoGets an object providing information necessary to paint a column header.
PainterGets the painter object that provides the default element painting mechanism. Inherited from CustomDrawObjectEventArgs.

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 CustomDrawColumnHeader event is raised each time a column header is about to be painted. The column is identified by the ColumnHeaderCustomDrawEventArgs.Column parameter. See the Custom Painting Basics and Custom Painting Scenarios topics for information on using custom draw events.

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.

Example

This example demonstrates how to fill the “Category_Name” column header with the Coral color.

csharp
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

private void Form3_Load1(object sender, EventArgs e) {
    CustomDrawColumnHeader(gridControl1, gridView1);
}

public static void CustomDrawColumnHeader(GridControl gridControl, GridView gridView) {
    // Handle this event to paint columns headers manually
    gridView.CustomDrawColumnHeader += (s, e) => {
        if (e.Column == null || e.Column.FieldName != "Category_Name")
            return;
        // Fill column headers with the specified colors.
        e.Cache.FillRectangle(Color.Coral, e.Bounds);
        e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect);
        // Draw the filter and sort buttons.
        foreach (DrawElementInfo info in e.Info.InnerElements) {
            if (!info.Visible) continue;
            ObjectPainter.DrawObject(e.Cache, info.ElementPainter, info.ElementInfo);
        }
        e.Handled = true;
    };
}
vb
Imports DevExpress.Utils
Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Private Sub Form3_Load1(ByVal sender As Object, ByVal e As EventArgs)
    CustomDrawColumnHeader(gridControl1, gridView1)
End Sub

Public Shared Sub CustomDrawColumnHeader(ByVal gridControl As GridControl, ByVal gridView As GridView)
    ' Handle this event to paint columns headers manually
    AddHandler gridView.CustomDrawColumnHeader, Sub(s, e)
        If e.Column Is Nothing OrElse e.Column.FieldName <> "Category_Name" Then
            Return
        End If
        ' Fill column headers with the specified colors.
        e.Cache.FillRectangle(Color.Coral, e.Bounds)
        e.Appearance.DrawString(e.Cache, e.Info.Caption, e.Info.CaptionRect)
        ' Draw the filter and sort buttons.
        For Each info As DrawElementInfo In e.Info.InnerElements
            If Not info.Visible Then
              Continue For
            End If
            ObjectPainter.DrawObject(e.Cache, info.ElementPainter, info.ElementInfo)
        Next info
        e.Handled = True
    End Sub
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomDrawColumnHeader 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-multiple-row-selection-web-style-checkboxes/CS/E1271/CheckMarkSelection.cs#L130

csharp
view.Click += new EventHandler(View_Click);
view.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
view.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);

winforms-grid-add-check-box-to-column-header/CS/GridViewColumnHeaderExtender.cs#L254

csharp
{
    view.CustomDrawColumnHeader -= OnCustomDrawColumnHeader;
    view.MouseDown -= OnMouseDown;

winforms-grid-custom-button-in-column-header/CS/WindowsApplication3/ColumnHeaderExtender.cs#L45

csharp
private void SubscribeToEvents() {
    view.CustomDrawColumnHeader += OnCustomDrawColumnHeader;
    view.MouseDown += OnMouseDown;

winforms-grid-display-editors-in-columns-headers/CS/WindowsApplication1/InplaceEditorHelper/ColumnInplaceEditorHelper.cs#L29

csharp
view = column.View as GridView;
view.CustomDrawColumnHeader += view_CustomDrawColumnHeader;
view.MouseDown += view_MouseDown;

winforms-grid-multiple-row-selection-web-style-checkboxes/VB/E1271/CheckMarkSelection.vb#L158

vb
AddHandler view.Click, New EventHandler(AddressOf View_Click)
AddHandler view.CustomDrawColumnHeader, New ColumnHeaderCustomDrawEventHandler(AddressOf View_CustomDrawColumnHeader)
AddHandler view.CustomDrawGroupRow, New RowObjectCustomDrawEventHandler(AddressOf View_CustomDrawGroupRow)

winforms-grid-add-check-box-to-column-header/VB/GridViewColumnHeaderExtender.vb#L234

vb
If Not subscribe Then
    RemoveHandler _view.CustomDrawColumnHeader, AddressOf OnCustomDrawColumnHeader
    RemoveHandler _view.MouseDown, AddressOf OnMouseDown

winforms-grid-custom-button-in-column-header/VB/WindowsApplication3/ColumnHeaderExtender.vb#L45

vb
Private Sub SubscribeToEvents()
    AddHandler view.CustomDrawColumnHeader, AddressOf OnCustomDrawColumnHeader
    AddHandler view.MouseDown, AddressOf OnMouseDown

winforms-grid-display-editors-in-columns-headers/VB/InplaceEditorHelper/ColumnInplaceEditorHelper.vb#L27

vb
view = TryCast(column.View, GridView)
AddHandler view.CustomDrawColumnHeader, AddressOf view_CustomDrawColumnHeader
AddHandler view.MouseDown, AddressOf view_MouseDown

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