删除 DataGridViewButtonColumn 周围的边框

Remove border around DataGridViewButtonColumn

我在这里找到了关于如何为 DataGridViewButtonColumn 设置图像的解决方案。我将列的 FlatStyle 设置为 Flat,现在我想摆脱按钮(删除按钮)周围的边界,但似乎没有 BorderSize 属性 对于 DataGridViewButtonColumn 我会设置为 0.

要绘制按钮图像,我使用

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

有什么帮助吗?

没有完整的代码,很难帮助你... 尝试删除以下行:

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

事实上,如果该列是用类似这样的代码获得的

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

e.Paint 行将绘制按钮(边框 + 填充颜色 + 文本)。但是,如果没有它,将不会呈现按钮设计。

您可能需要在图像后面绘制一个矩形作为背景色。

A DataGridViewButtonCellFlatStyle 属性 设置为 FlatStyle.Flat 使用单元格的 ForeColorSelectionForeColor 绘制边框部分。您可以在源代码中找到,how the border color is selected and passed 到绘制边框方法。因此,消除边框的最简单解决方法是将单元格的 BackColor 分配给 ForeColorSelectionForeColor 属性。

使用设计器应用那个或通过代码:

Public Class YourForm

    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

或者,接管并按照您喜欢的方式自己绘制整个事物:

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