Home > Software engineering >  Remove border around DataGridViewButtonColumn
Remove border around DataGridViewButtonColumn

Time:12-20

I found a solution here on how to set an image for a DataGridViewButtonColumn. I set the FlatStyle of the column to Flat and now I want to get rid of the boarder around the button (the Delete button) but there does not seem to be a BorderSize property for DataGridViewButtonColumn which I would have set to 0.

enter image description here

To draw the button image, I use

If dgvPaymentList.Columns(e.ColumnIndex).Name = "xDelete" AndAlso e.RowIndex >= 0 Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All)
        e.Graphics.DrawImage(My.Resources.delete16White, CInt((e.CellBounds.Width / 2) - (My.Resources.delete16White.Width / 2))   e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.delete16White.Height / 2))   e.CellBounds.Y)
        e.Handled = True
End If

Any help?

CodePudding user response:

Without the full code it's a little hard to help you... Try removing the following line:

e.Paint(e.CellBounds, DataGridViewPaintParts.All)

In fact, if the column was obtained with a code similar to this

Dim btn As New DataGridViewButtonColumn() With {
    .HeaderText = "Example",
    .Text = "Click Me",
    .UseColumnTextForButtonValue = True,
    .FlatStyle = FlatStyle.Flat 'Or others
}
DataGridView1.Columns.Add(btn)

the e.Paint line will draw the button (border fillcolor text). Without it, however, the button design will not be rendered.

You may need to draw a rectangle behind the image for the background color.

CodePudding user response:

A DataGridViewButtonCell with the FlatStyle property set to FlatStyle.Flat uses the cell's ForeColor and SelectionForeColor to draw the border part. You can find that in the source code, how the border color is selected and passed to the draw border method. Hence, the easiest workaround to get rid of the border is to assign the cell's BackColor to the ForeColor and SelectionForeColor properties.

Use the designer to apply that or through the code:

Public Class Form2

    Private ReadOnly img As Bitmap

    Sub New()
        InitializeComponent()

        img = My.Resources.delete16White

        Dim c = Color.FromArgb(64, 64, 64)

        ' Your button column...
        xDelete.DefaultCellStyle.BackColor = c
        xDelete.DefaultCellStyle.SelectionBackColor = c
        xDelete.DefaultCellStyle.ForeColor = c
        xDelete.DefaultCellStyle.SelectionForeColor = c
    End Sub

    Protected Overrides Sub OnClosed(e As EventArgs)
        MyBase.OnClosed(e)
        img.Dispose()
    End Sub

    Private Sub dgvPaymentList_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvPaymentList.CellPainting
        If e.RowIndex >= 0 AndAlso e.ColumnIndex = dgvcButton.Index Then
            Dim r = e.CellBounds
            Dim imgRec = New Rectangle(
                r.X   (r.Width - img.Width) \ 2,
                r.Y   (r.Height - img.Height) \ 2,
                img.Width, img.Height)

            e.Paint(e.ClipBounds, DataGridViewPaintParts.All)
            e.Graphics.DrawImage(img, imgRec)
            e.Handled = True
        End If
    End Sub
End Class

Alternatively, takeover and draw the whole thing yourself the way you like:

Private Sub dgvPaymentList_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvPaymentList.CellPainting
    If e.RowIndex >= 0 AndAlso e.ColumnIndex = xDelete.Index Then
        Dim r = e.CellBounds
        Dim imgRec = New Rectangle(
            r.X   (r.Width - img.Width) \ 2,
            r.Y   (r.Height - img.Height) \ 2,
            img.Width, img.Height)

        e.PaintBackground(e.ClipBounds, False)

        If e.CellBounds.Contains(dgvPaymentList.PointToClient(MousePosition)) Then
            Dim backColor As Color

            If MouseButtons = MouseButtons.Left Then
                backColor = Color.FromArgb(104, 104, 104)
            Else
                backColor = Color.FromArgb(84, 84, 84)
            End If

            Using br As New SolidBrush(backColor)
                e.Graphics.FillRectangle(br, r)
            End Using
        End If

        e.Graphics.DrawImage(img, imgRec)
        e.Handled = True
    End If
End Sub
  • Related