Back to Devexpress

SpreadsheetControl.CustomDrawColumnHeader Event

windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-aad6307a.md

latest10.0 KB
Original Source

SpreadsheetControl.CustomDrawColumnHeader Event

Enables the column header to be painted manually.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.XtraSpreadsheet.v25.2.dll

NuGet Package : DevExpress.Win.Spreadsheet

Declaration

csharp
public event CustomDrawColumnHeaderEventHandler CustomDrawColumnHeader
vb
Public Event CustomDrawColumnHeader As CustomDrawColumnHeaderEventHandler

Event Data

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

PropertyDescription
AppearanceProvides access to the properties that control the appearance of a worksheet header. Inherited from CustomDrawHeaderEventArgsBase.
BackColorGets the background color of the header. Inherited from CustomDrawHeaderEventArgsBase.
BoundsGets the header’s bound rectangle. Inherited from CustomDrawHeaderEventArgsBase.
CacheGets an object that serves as the storage for pens, fonts and brushes. Inherited from CustomDrawObjectEventsArgs.
ColumnIndexReturns the column index of the column header being painted.
ControlProvides access to the SpreadsheetControl that raised the event. Inherited from CustomDrawHeaderEventArgsBase.
FontGets the font used to paint the header caption. Inherited from CustomDrawHeaderEventArgsBase.
ForeColorGets the color used to paint the header caption text. Inherited from CustomDrawHeaderEventArgsBase.
GraphicsGets an object used for painting. Inherited from CustomDrawObjectEventsArgs.
HandledGets or sets whether an event is handled. If true, the default actions are not required. Inherited from CustomDrawObjectEventsArgs.
IsHoveredGets whether a mouse is currently over the worksheet header. Inherited from CustomDrawHeaderEventArgsBase.
IsSelectedGets whether the current column contains selected cell(s). Inherited from CustomDrawHeaderEventArgsBase.
TextGets the text of the header caption. Inherited from CustomDrawHeaderEventArgsBase.

The event data class exposes the following methods:

MethodDescription
DrawDefault()Renders the element using the default drawing mechanism. Inherited from CustomDrawObjectEventsArgs.

Remarks

The CustomDrawColumnHeader event is raised before a column header is painted. Event arguments properties provide the objects and information required to paint the column header.

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 code snippet below handles the SpreadsheetControl.CustomDrawColumnHeader event to change default text (A, B, C, …) in column headers. This example does not change the background color of column headers. Refer to the following topic for information on how to apply a custom color to column headers: SpreadsheetControl.CustomDrawColumnHeaderBackground.

View Example: Use Custom Draw Events to Customize the Appearance of Spreadsheet Elements

csharp
using System;
using System.Drawing;
using DevExpress.Spreadsheet;
using DevExpress.XtraSpreadsheet;
// ...
private void spreadsheetControl1_CustomDrawColumnHeader(object sender, 
    CustomDrawColumnHeaderEventArgs e)
{           
    // Cancel default painting for the column header.
    e.Handled = true;

    if (e.ColumnIndex > 4) 
        return;

    // Specify the list of new column header names.
    string[] headers = new string[] { "Category", "Product Name", 
        "Quantity per Unit", "Unit Price", "Units in Stock" };

    // Specify font attributes for the column header.
    SpreadsheetFont defaultFont = spreadsheetControl1.Document.Styles.DefaultStyle.Font;
    using (Font font = new Font(defaultFont.Name, (float)defaultFont.Size, FontStyle.Bold))
    {
        // Specify layout settings for the column header.
        using (StringFormat stringFormat = new StringFormat())
        {
            stringFormat.LineAlignment = StringAlignment.Center;
            stringFormat.Alignment = e.ColumnIndex < 3 ? StringAlignment.Near : StringAlignment.Far;
            stringFormat.Trimming = StringTrimming.EllipsisCharacter;

            // Draw the column header.
            e.Graphics.DrawString(headers[e.ColumnIndex], font, 
                e.Cache.GetSolidBrush(Color.White), e.Bounds, stringFormat);
        }
    }
}
vb
Imports System
Imports System.Drawing
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet
' ...
Private Sub spreadsheetControl1_CustomDrawColumnHeader(ByVal sender As Object,
    ByVal e As CustomDrawColumnHeaderEventArgs)
    ' Cancel default painting for the column header.
    e.Handled = True

    If e.ColumnIndex > 4 Then
        Return
    End If

    ' Specify the list of new column header names.
    Dim headers() As String = {"Category", "Product Name", "Quantity per Unit",
        "Unit Price", "Units in Stock"}

    ' Specify font attributes for the column header.
    Dim defaultFont As SpreadsheetFont = spreadsheetControl1.Document.Styles.DefaultStyle.Font
    Using font As New Font(defaultFont.Name, CSng(defaultFont.Size), FontStyle.Bold)
        ' Specify layout settings for the column header.
        Using stringFormat As New StringFormat()
            stringFormat.LineAlignment = StringAlignment.Center
            stringFormat.Alignment = If(e.ColumnIndex < 3, StringAlignment.Near, StringAlignment.Far)
            stringFormat.Trimming = StringTrimming.EllipsisCharacter

            ' Draw the column header.
            e.Graphics.DrawString(headers(e.ColumnIndex), font,
        e.Cache.GetSolidBrush(Color.White), e.Bounds, stringFormat)
        End Using
    End Using
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference 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-spreadsheet-custom-draw-example/VB/CustomDrawExample/Form1.vb#L19

vb
AddHandler spreadsheetControl1.CustomDrawColumnHeader, AddressOf spreadsheetControl1_CustomDrawColumnHeader
AddHandler spreadsheetControl1.CustomDrawColumnHeaderBackground, AddressOf spreadsheetControl1_CustomDrawColumnHeaderBackground

See Also

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.XtraSpreadsheet Namespace