Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Custom draw cells

The following sample code handles the TreeList.CustomDrawNodeCell event. It is used to perform custom painting of node cells. All node cells are painted in the same manner.

The image below illustrates the result of executing the sample code.

CustomDraw - NodeCell

using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraTreeList;

private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e) {
    // Create brushes for cells.
    Brush backBrush, foreBrush;
    if (e.Node != (sender as TreeList).FocusedNode)
    {
        backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.PapayaWhip, Color.PeachPuff, LinearGradientMode.ForwardDiagonal);
        foreBrush = Brushes.Black;
    }
    else
    {
        backBrush = Brushes.DarkBlue;
        foreBrush = e.Cache.GetSolidBrush(Color.PeachPuff);
    }
    // Fill the background.
    e.Cache.FillRectangle(backBrush, e.Bounds);
    // Paint the node value.
    e.Cache.DrawString(e.CellText, e.Appearance.Font, foreBrush, e.Bounds,
      e.Appearance.GetStringFormat());

    // Prohibit default painting.
    e.Handled = true;
}