| FlexGrid for WinForms Task-Based Help > Applying a Gradient Background to a CellRange |
To apply a gradient background to a CellRange, use the OwnerDrawCell event to create custom cell painting.
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Dim GradientStyleBrush As System.Drawing.Drawing2D.LinearGradientBrush Dim rng As C1.Win.C1FlexGrid.CellRange |
|
To write code in C#
| C# |
Copy Code
|
|---|---|
System.Drawing.Drawing2D.LinearGradientBrush GradientStyleBrush; C1.Win.C1FlexGrid.CellRange rng; |
|
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Me.C1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw |
|
To write code in C#
| C# |
Copy Code
|
|---|---|
this.c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw; |
|
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
rng = Me.C1FlexGrid1.GetCellRange(2, 2, 4, 4) |
|
To write code in C#
| C# |
Copy Code
|
|---|---|
rng = this.c1FlexGrid1.GetCellRange(2, 2, 4, 4); |
|
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
GradientStyleBrush = New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270) |
|
To write code in C#
| C# |
Copy Code
|
|---|---|
GradientStyleBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Navy, Color.Transparent, 270); |
|
To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
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
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
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;
}
}
}
|
|
The Transparent to Navy gradient background appears only in the CellRange.