Skip to main content Skip to footer

How to Draw Gradient Cell Background

To apply a gradient background to a CellRange, use the OwnerDrawCell event to create custom cell painting. This code snippet applies a gradient cell background to a small cell range.

this.c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;
C1.Win.C1FlexGrid.CellRange rng = this.c1FlexGrid1.GetCellRange(2, 2, 4, 4);
System.Drawing.Drawing2D.LinearGradientBrush GradientStyleBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270);

private void c1FlexGrid1_OwnerDrawCell(object sender, C1.Win.C1FlexGrid.OwnerDrawCellEventArgs e)  
{  
    // Draw cell background using gradient brush.  
    if ((e.Row >= rng.r1) & (e.Row <= rng.r2))  
    {  
        if ((e.Col >= rng.c1) & (e.Col <= rng.c2))  
        {  
            // Draw background.  
            e.Graphics.FillRectangle(GradientStyleBrush, e.Bounds);  

            // Let the grid draw the content.  
            e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Content);  

            // Finish drawing the cell.  
            e.Handled = true;  
        }  
    }  
}