Applying a Gradient Background to a CellRange

To apply a gradient background to a CellRange, use the OwnerDrawCell event to create custom cell painting.

  1. Create a LinearGradient brush and a CellRange by adding the following code to the form class level: Visual Basic:

    Dim GradientStyleBrush As System.Drawing.Drawing2D.LinearGradientBrush  
    Dim rng As C1.Win.C1FlexGrid.CellRange
    
[C#](http://helpcentral.componentone.com/nethelp/c1flexgrid/applyingagradientbackground.html#):  

~~~
System.Drawing.Drawing2D.LinearGradientBrush GradientStyleBrush;  
C1.Win.C1FlexGrid.CellRange rng;
~~~
  1. Set C1FlexGrid's DrawMode property to OwnerDraw by adding the following code to the Form_Load event:
    Visual Basic:

    Me.C1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw
    
[C#](http://helpcentral.componentone.com/nethelp/c1flexgrid/applyingagradientbackground.html#):  

~~~
this.c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;
~~~
  1. Set the CellRange using the GetCellRange method:
    Visual Basic:

    rng = Me.C1FlexGrid1.GetCellRange(2, 2, 4, 4)
    
[C#](http://helpcentral.componentone.com/nethelp/c1flexgrid/applyingagradientbackground.html#):  

~~~
rng = this.c1FlexGrid1.GetCellRange(2, 2, 4, 4);
~~~
  1. Set the LinearGradient brush colors and angle of the gradient: Visual Basic:

    GradientStyleBrush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270)
    
[C#](http://helpcentral.componentone.com/nethelp/c1flexgrid/applyingagradientbackground.html#):  

~~~
GradientStyleBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270);
~~~
  1. Add the OwnerDrawCell event to draw the gradient in the CellRange: Visual Basic:

    Private Sub C1FlexGrid1_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Handles C1FlexGrid1.OwnerDrawCell  
    
        ' Draw cell background using gradient brush.  
        If (e.Row >= rng.r1) And (e.Row <= rng.r2) Then  
            If (e.Col >= rng.c1) And (e.Col <= rng.c2) Then  
    
                ' 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  
            End If  
         End If  
    End Sub
    
[C#](http://helpcentral.componentone.com/nethelp/c1flexgrid/applyingagradientbackground.html#):  

~~~
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;  
        }  
    }  
}
~~~

This topic illustrates the following:

The Transparent to Navy gradient background appears only in the CellRange.

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus