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#:

    System.Drawing.Drawing2D.LinearGradientBrush GradientStyleBrush;
    C1.Win.C1FlexGrid.CellRange rng;



  2. 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#:

    this.c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;




  3. Set the CellRange using the GetCellRange method:


    Visual Basic:

    rng = Me.C1FlexGrid1.GetCellRange(2, 2, 4, 4)



    C#:

    rng = this.c1FlexGrid1.GetCellRange(2, 2, 4, 4);




  4. 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#:

    GradientStyleBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270);




  5. 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

    <span style="color: green;">' Draw cell background using gradient brush.</span>
    If (e.Row >= rng.r1) And (e.Row <= rng.r2) Then
    If (e.Col >= rng.c1) And (e.Col <= rng.c2) Then

    <span style="color: green;">' Draw background.</span>
    e.Graphics.FillRectangle(GradientStyleBrush, e.Bounds)

    <span style="color: green;">' Let the grid draw the content.</span>
    e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Content)

    <span style="color: green;">' Finish drawing the cell.</span>
    e.Handled = True
    End If
    End If
    End Sub



    C#:

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

    <span style="color: green;">// Let the grid draw the content.</span>
    e.DrawCell(C1.Win.C1FlexGrid.DrawCellFlags.Content);

    <span style="color: green;">// Finish drawing the cell.</span>
    e.Handled = true;
    }
    }
    }





This topic illustrates the following:


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