How to: Custom Paint Cells

  • 2 minutes to read

The following example shows how to custom paint cells (normal, focused, selected and Grand Total cells) within the Pivot Grid Control's Data Area. For this purpose the PivotGridControl.CustomDrawCell event is handled. The event parameter's properties allow the processed cell to be identified.

The image below shows the result.

cdCustomPainting

using System.Drawing.Drawing2D;
using DevExpress.XtraPivotGrid;
// ...

private void pivotGridControl1_CustomDrawCell(object sender, PivotCustomDrawCellEventArgs e) {
    Rectangle r;

    // Paints Row Grand Totals.
    if(e.RowValueType == PivotGridValueType.GrandTotal) {
        e.GraphicsCache.FillRectangle(new LinearGradientBrush(e.Bounds, Color.LightBlue,
            Color.Blue, LinearGradientMode.Vertical), e.Bounds);
        r = Rectangle.Inflate(e.Bounds, -3, -3);
        e.GraphicsCache.FillRectangle(new LinearGradientBrush(e.Bounds, Color.Blue,
            Color.LightSkyBlue, LinearGradientMode.Vertical), r);
        e.GraphicsCache.DrawString(e.DisplayText, e.Appearance.Font, 
            new SolidBrush(Color.White), r, e.Appearance.GetStringFormat());
        e.Handled = true;
        return;
    }

    // Paints the data cells.
    Brush backBrush;
    r = e.Bounds;

            if (e.Focused)
                // Initializes the brush used to paint the focused cell.
                backBrush = e.GraphicsCache.GetSolidBrush(Color.Pink);
            else
                if (e.Selected)
                    // Initializes the brush used to paint selected cells.
                    backBrush = e.GraphicsCache.GetSolidBrush(Color.PaleGreen);
                else
                    // Initializes the brush used to paint data cells.
                    backBrush = e.GraphicsCache.GetSolidBrush(Color.LemonChiffon);

            e.GraphicsCache.DrawRectangle(new Pen(new SolidBrush(Color.DimGray), 1), r);
            r.Inflate(-1, -1);
            e.GraphicsCache.FillRectangle(backBrush, r);
            e.Appearance.DrawString(e.GraphicsCache, e.DisplayText, r);

    e.Handled = true;
}