Smith
Smith

Reputation: 5961

change datagridview cellstyle border color

I am trying to change the cell border colors based on the background color of the cell

this is what i have used

'Draw custom cell borders.
        Using Brush As New SolidBrush(grdList.ColumnHeadersDefaultCellStyle.BackColor)
            e.Graphics.FillRectangle(Brush, e.CellBounds)
        End Using

        e.Paint(e.CellBounds, DataGridViewPaintParts.All And Not DataGridViewPaintParts.ContentBackground)
        Debug.Print(e.CellStyle.BackColor.ToString)
        ControlPaint.DrawBorder(e.Graphics, e.CellBounds, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, e.CellStyle.BackColor, 1, _
                                ButtonBorderStyle.Solid, Color.Black, 1, _
                                 ButtonBorderStyle.Solid)

this is the result

enter image description here

i dont what the white lines currently seen

Upvotes: 0

Views: 5353

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

As an option you can set border styles yo none:

Me.DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None

Then handle CellPainting event and paint borders:

Private Sub DataGridView1_CellPainting(sender As Object, _
    e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

    If (e.ColumnIndex < 0 OrElse e.RowIndex < 0) Then Return
    e.Paint(e.CellBounds, DataGridViewPaintParts.All)
    Dim r = e.CellBounds
    e.Graphics.DrawLine(Pens.Black, r.Left, r.Top, r.Right, r.Top)
    e.Graphics.DrawLine(Pens.Black, r.Left, r.Bottom, r.Right, r.Bottom)
    e.Handled = True
End Sub

enter image description here

Upvotes: 2

Related Questions