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 DevExpress.XtraPivotGrid;
using System.Drawing;
// ...

private void pivotGridControl1_CustomDrawCell(object sender, PivotCustomDrawCellEventArgs e) {
    Rectangle r;
    // Paints Row Grand Totals.
    if (e.RowValueType == PivotGridValueType.GrandTotal)
    {
        Brush brushFillTotals;
        brushFillTotals = e.GraphicsCache.GetSolidBrush(ColorTranslator.FromHtml("#0099cc"));
        r = e.Bounds;
        e.GraphicsCache.FillRectangle(brushFillTotals, e.Bounds);
        r.Inflate(-4, -4);
        e.GraphicsCache.DrawString(e.DisplayText, e.Appearance.Font,
        Brushes.White, r, e.Appearance.GetStringFormat());
        e.Handled = true;
        return;
    }

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

    if (e.Focused)
        // Initializes the brush used to paint the focused cell.
        brushFill = e.GraphicsCache.GetSolidBrush(Color.White);
    else
        if (e.Selected)
        // Initializes the brush used to paint selected cells.
        brushFill = e.GraphicsCache.GetSolidBrush(ColorTranslator.FromHtml("#b6e7eb"));
    else
        // Initializes the brush used to paint data cells.
        brushFill = e.GraphicsCache.GetSolidBrush(ColorTranslator.FromHtml("#ecfbf8"));

    e.GraphicsCache.DrawRectangle(Pens.DimGray, r);
    r.Inflate(-1, -1);
    e.GraphicsCache.FillRectangle(brushFill, r);
    if (e.Focused)
        {
            r.Inflate(-1, -1);
            e.GraphicsCache.DrawRectangle(e.GraphicsCache.GetPen(ColorTranslator.FromHtml("#f05b41"), 3), r);

        }
    r.Inflate(-4, -4);
    e.Appearance.DrawString(e.GraphicsCache, e.DisplayText, r);

    e.Handled = true;
}