Home > Software design >  Change line color with gradient in a DataGridView
Change line color with gradient in a DataGridView

Time:08-12

Is it possible to change the rows of a DataGridView to put a color that is not solid? Preferably a gradient?

I tried changing the RowsDefaultCellStyle property but it doesn't work, it only has the solid color option.

CodePudding user response:

You can try something like this, using the grid's "CellPainting" event

Private Sub DataGridView_CellPainting(ByVal oDataGridView As DataGridView, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
    If e.RowIndex <> -1 Then
        Dim oLinearGradientBrush As System.Drawing.Drawing2D.LinearGradientBrush
        Dim oColor As Color = System.Drawing.Color.FromArgb(255, 204, 0)
        Dim oColorB As Color = System.Drawing.Color.FromArgb(255, 236, 159)

        oLinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.CellBounds, oColorB, oColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        Dim Width As Integer = 0
        For Each col As DataGridViewColumn In oDataGridView.Columns
            If col.Visible Then
                Width = Width   col.Width
            End If
        Next
        Dim b As New System.Drawing.RectangleF(e.CellBounds.X, e.CellBounds.Y, Width, e.CellBounds.Height)
        e.Graphics.FillRectangle(oLinearGradientBrush, b)
        oLinearGradientBrush.Dispose()
        e.Paint(e.CellBounds, DataGridViewPaintParts.Border Or DataGridViewPaintParts.ContentForeground)
        e.Handled = True
    End If
End Sub

A detail, when selecting the line the code below changes the color a little to highlight it. Using gradient too. For that use the event "RowPrePaint" of the grid.

Public Sub DataGridView_RowPrePaint(ByVal oDataGridView As DataGridView, ByVal e As DataGridViewRowPrePaintEventArgs)

    If oDataGridView.Rows(e.RowIndex).Selected And oDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect Then
        Dim oLinearGradientBrush As System.Drawing.Drawing2D.LinearGradientBrush
        Dim oColor As Color = System.Drawing.Color.FromArgb(255, 204, 0)
        Dim oColorB As Color = System.Drawing.Color.FromArgb(255, 236, 159)

        oLinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(e.RowBounds, oColorB, oColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        Dim Width As Integer = 0
        For Each col As DataGridViewColumn In oDataGridView.Columns
            If col.Visible Then
                Width = Width   col.Width
            End If
        Next
        Dim b As New System.Drawing.RectangleF(e.RowBounds.X, e.RowBounds.Y, Width, e.RowBounds.Height)
        e.Graphics.FillRectangle(oLinearGradientBrush, b) 'e.RowBounds)
        oLinearGradientBrush.Dispose()
        e.PaintCells(e.RowBounds, DataGridViewPaintParts.Border Or DataGridViewPaintParts.ContentForeground)
        e.Handled = True
    End If


End Sub

This example will help you in what you need, but I strongly recommend that you create a Component and define your grid inside it, it's the best way to do it.

  • Related