VB.NET
Private Sub dgv1_CellPainting(ByVal sender As Object, _
ByVal e As DataGridViewCellPaintingEventArgs) _
Handles dgv1.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 AndAlso _
(e.PaintParts And DataGridViewPaintParts.Background) = _
DataGridViewPaintParts.Background Then
Dim bColor1, bColor2 As Color
If (e.PaintParts And DataGridViewPaintParts.SelectionBackground) = _
DataGridViewPaintParts.SelectionBackground AndAlso _
(e.State And DataGridViewElementStates.Selected) = _
DataGridViewElementStates.Selected Then
bColor1 = e.CellStyle.SelectionBackColor
bColor2 = Color.Black
Else
bColor1 = e.CellStyle.BackColor
bColor2 = Color.LemonChiffon
End If
Dim b As New System.Drawing.Drawing2D.LinearGradientBrush( _
e.CellBounds, bColor1, bColor2, _
System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
Try
e.Graphics.FillRectangle(b, e.CellBounds)
Finally
b.Dispose()
End Try
Dim paintParts As DataGridViewPaintParts = _
e.PaintParts And Not DataGridViewPaintParts.Background
e.Paint(e.ClipBounds, paintParts)
e.Handled = True
End If
End Sub
C#private void dgv1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0 &&
(e.PaintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
Color bColor1, bColor2;
if ((e.PaintParts & DataGridViewPaintParts.SelectionBackground) ==
DataGridViewPaintParts.SelectionBackground &&
(e.State & DataGridViewElementStates.Selected) ==
DataGridViewElementStates.Selected)
{
bColor1 = e.CellStyle.SelectionBackColor;
bColor2 = Color.Black;
}
else
{
bColor1 = e.CellStyle.BackColor;
bColor2 = Color.LemonChiffon;
}
using (System.Drawing.Drawing2D.LinearGradientBrush b =
new System.Drawing.Drawing2D.LinearGradientBrush(
e.CellBounds, bColor1, bColor2,
System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(b, e.CellBounds);
}
DataGridViewPaintParts paintParts =
e.PaintParts & ~DataGridViewPaintParts.Background;
e.Paint(e.ClipBounds, paintParts);
e.Handled = true;
}
}