Back to Devexpress

SpreadsheetControl.CustomDrawRowHeader Event

windowsforms-devexpress-dot-xtraspreadsheet-dot-spreadsheetcontrol-9bcd3725.md

latest9.1 KB
Original Source

SpreadsheetControl.CustomDrawRowHeader Event

Enables the row header to be painted manually.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.XtraSpreadsheet.v25.2.dll

NuGet Package : DevExpress.Win.Spreadsheet

Declaration

csharp
public event CustomDrawRowHeaderEventHandler CustomDrawRowHeader
vb
Public Event CustomDrawRowHeader As CustomDrawRowHeaderEventHandler

Event Data

The CustomDrawRowHeader event's data class is CustomDrawRowHeaderEventArgs. 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.
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.
RowIndexReturns the column index of the row header being painted.
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 CustomDrawRowHeader event is raised before a row header is painted. Event arguments properties provide the objects and information required to paint the row 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 uses the SpreadsheetControl.CustomDrawRowHeader event to display a row number for every fifth row.

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_CustomDrawRowHeader(object sender, CustomDrawRowHeaderEventArgs e)
{
    // Cancel default painting for the row header.
    e.Handled = true;

    // Display a row number for every fifth row.
    if ((e.RowIndex + 1) % 5 == 0)
    {
        // Specify font style.
        e.Appearance.FontStyleDelta = FontStyle.Bold;

        // Return the row header bounds.
        Rectangle textBounds = e.Bounds;

        // Specify row header text.
        string text = (e.RowIndex + 1).ToString();

        // Specify text alignment. 
        StringFormat format = new StringFormat {
            LineAlignment = StringAlignment.Center,
            Alignment = StringAlignment.Center
        };

        // Draw row header text.
        e.Graphics.DrawString(text, e.Font, 
            e.Cache.GetSolidBrush(Color.FromArgb(91, 155, 213)), textBounds, format);
    }
}
vb
Imports System
Imports System.Drawing
Imports DevExpress.Spreadsheet
Imports DevExpress.XtraSpreadsheet
' ...
Private Sub spreadsheetControl1_CustomDrawRowHeader(ByVal sender As Object,
    ByVal e As CustomDrawRowHeaderEventArgs)
    ' Cancel default painting for the row header.
    e.Handled = True

    ' Display a row number for every fifth row.
    If (e.RowIndex + 1) Mod 5 = 0 Then
        ' Specify font style.
        e.Appearance.FontStyleDelta = FontStyle.Bold

        ' Return the row header bounds.
        Dim textBounds As Rectangle = e.Bounds

        ' Specify row header text.
        Dim text As String = (e.RowIndex + 1).ToString()

        ' Specify text alignment. 
        Dim format As New StringFormat With {
            .LineAlignment = StringAlignment.Center,
            .Alignment = StringAlignment.Center
        }

        ' Draw row header text.
        e.Graphics.DrawString(text, e.Font,
    e.Cache.GetSolidBrush(Color.FromArgb(91, 155, 213)), textBounds, format)
    End If
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomDrawRowHeader 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#L21

vb
AddHandler spreadsheetControl1.CustomDrawColumnHeaderBackground, AddressOf spreadsheetControl1_CustomDrawColumnHeaderBackground
AddHandler spreadsheetControl1.CustomDrawRowHeader, AddressOf spreadsheetControl1_CustomDrawRowHeader
AddHandler spreadsheetControl1.CustomDrawRowHeaderBackground, AddressOf spreadsheetControl1_CustomDrawRowHeaderBackground

See Also

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.XtraSpreadsheet Namespace