1

Good afternoon!

There is a custom class that adds a text column with an image to the DataGridView.

Public Class DataGridViewTextAndImageColumn
    Inherits DataGridViewTextBoxColumn
    Private imageValue As Image
    Private imageSize_Renamed As Size

    Public Sub New()
        CellTemplate = New TextAndImageCell()
    End Sub

    Public Overrides Function Clone() As Object
        Dim c As DataGridViewTextAndImageColumn = TryCast(MyBase.Clone(), DataGridViewTextAndImageColumn)
        c.imageValue = imageValue
        c.imageSize_Renamed = imageSize_Renamed
        Return c
    End Function

    Public Property Image() As Image
        Get
            Return imageValue
        End Get
        Set(ByVal value As Image)
            If Image IsNot value Then
                imageValue = value
                imageSize_Renamed = value.Size

                If InheritedStyle IsNot Nothing Then
                    Dim inheritedPadding As Padding = InheritedStyle.Padding
                    DefaultCellStyle.Padding = New Padding(imageSize_Renamed.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
                End If
            End If
        End Set
    End Property

    Friend ReadOnly Property ImageSize() As Size
        Get
            Return imageSize_Renamed
        End Get
    End Property
End Class

Public Class TextAndImageCell
    Inherits DataGridViewTextBoxCell

    Private imageValue As Image
    Private imageSize As Size

    Public Overrides Function Clone() As Object
        Dim c As TextAndImageCell = TryCast(MyBase.Clone(), TextAndImageCell)
        c.imageValue = imageValue
        c.imageSize = imageSize
        Return c
    End Function

    Public Property Image() As Image
        Get
            If OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then

                Return imageValue
            ElseIf imageValue IsNot Nothing Then
                Return imageValue
            Else
                Return OwningTextAndImageColumn.Image
            End If
        End Get
        Set(ByVal value As Image)
            If imageValue IsNot value Then
                imageValue = value
                imageSize = value.Size

                Dim inheritedPadding As Padding = InheritedStyle.Padding
                Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
            End If
        End Set
    End Property

    Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
        ' Paint the base content
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)

        If Image IsNot Nothing Then
            ' Draw the image clipped to the cell.
            Dim container As Drawing2D.GraphicsContainer = graphics.BeginContainer()

            graphics.SetClip(cellBounds)
            graphics.DrawImageUnscaled(Image, cellBounds.Location)

            graphics.EndContainer(container)
        End If
    End Sub

    Private ReadOnly Property OwningTextAndImageColumn() As DataGridViewTextAndImageColumn
        Get
            Return TryCast(OwningColumn, DataGridViewTextAndImageColumn)
        End Get
    End Property
End Class

Form:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
             With DataGridView1
            .DefaultCellStyle.WrapMode = DataGridViewTriState.True
            .Columns(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        End With

    End Sub
End Class

Result of work:

enter image description here

The question is:

The question is:

When a column is line-wrapped, the text is centered vertically in the cell. How to make a picture in a cell also centered regardless of the line height?

enter image description here

Lider13
  • 47
  • 7
  • 2
    Change `graphics.DrawImageUnscaled(Image, cellBounds.Location)`, so you center vertically the image inside the provided `cellBounds`. -- This code, which set a GraphicContainer, seems to be designed to do something different from what you actually have here. – Jimi Jun 14 '22 at 11:57
  • 1
    BTW, careful with `DrawImageUnscaled()` when Dpi or Font scaling changes. You may end up with an image too small or too large. You should probably calculate a measure based on the average height of a single-line Row (when you receive DPI change or User Settings change notifications). – Jimi Jun 14 '22 at 12:07
  • Thank you for your responses. Could you show with an example how this can be done? – Lider13 Jun 14 '22 at 12:28
  • 1
    An example of what? Centering the image vertically? That's `new Point(cellBounds.Left, (cellBounds.Height - [Image Bounds].Height) / 2)`. Or use the `clipBounds` value, depending on the minimum size of the Column. – Jimi Jun 14 '22 at 12:38

0 Answers0