Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:37:15+00:00 2026-06-06T11:37:15+00:00

I’ve used the following code, taken from here , to create my own custom

  • 0

I’ve used the following code, taken from here, to create my own custom column for datagridview, so that I can now have images and text in the same cell:

Public Class TextAndImageColumn
    Inherits DataGridViewTextBoxColumn
    Private imageValue As Image
    Private m_imageSize As Size
    Public Sub New()
        Me.CellTemplate = New TextAndImageCell
    End Sub
    Public Overloads Overrides Function Clone() As Object
        Dim c As TextAndImageColumn = TryCast(MyBase.Clone, TextAndImageColumn)
        c.imageValue = Me.imageValue
        c.m_imageSize = Me.m_imageSize
        Return c
    End Function
    Public Property Image() As Image
        Get
            Return Me.imageValue
        End Get
        Set(ByVal value As Image)
            Me.imageValue = value
            Me.m_imageSize = value.Size
            Dim inheritedPadding As Padding = Me.DefaultCellStyle.Padding
            Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
        End Set
    End Property
    Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
        Get
            Return TryCast(Me.CellTemplate, TextAndImageCell)
        End Get
    End Property
    Friend ReadOnly Property ImageSize() As Size
        Get
            Return m_imageSize
        End Get
    End Property
End Class
Public Class TextAndImageCell
    Inherits DataGridViewTextBoxCell
    Private imageValue As Image
    Private imageSize As Size
    Public Overloads Overrides Function Clone() As Object
        Dim c As TextAndImageCell = TryCast(MyBase.Clone, TextAndImageCell)
        c.imageValue = Me.imageValue
        c.imageSize = Me.imageSize
        Return c
    End Function
    Public Property Image() As Image
        Get
            If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
                Return imageValue
            Else
                If Not (Me.imageValue Is Nothing) Then
                    Return Me.imageValue
                Else
                    Return Me.OwningTextAndImageColumn.Image
                End If
            End If
        End Get
        Set(ByVal value As Image)
            Me.imageValue = value
            Me.imageSize = value.Size
            Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
            Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
        End Set
    End Property
    Protected Overloads Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle,
                                            rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, formattedValue As Object,
                                            errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle,
                                            paintParts As DataGridViewPaintParts)
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
        If Not (Me.Image Is Nothing) Then
            Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
            graphics.SetClip(cellBounds)
            graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
            graphics.EndContainer(container)
        End If
    End Sub
    Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
        Get
            Return TryCast(Me.OwningColumn, TextAndImageColumn)
        End Get
    End Property
End Class

It works very well, except that the image that I use is right at the edges of the cell. I’d like to give it a small margin. How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:37:16+00:00Added an answer on June 6, 2026 at 11:37 am

    You could add a property to TextAndImageCell like :

    Private m_imagePadding As New Padding(3)
    
    Public Property ImagePadding() As Padding
        Get
            Return m_imagePadding
        End Get
        Set(ByVal value As Padding)
            m_imagePadding = value
        End Set
    End Property
    

    and implement (in Paint) like :

    graphics.DrawImageUnscaled(Me.Image, _
             New Point(cellBounds.Location.X + m_imagePadding.Left, _
                       cellBounds.Location.Y + m_imagePadding.Top))
    

    also would need to change in TextAndImageColumn :

    Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width + _
       TextAndImageCellTemplate.ImagePadding.Right, inheritedPadding.Top, _
       inheritedPadding.Right, inheritedPadding.Bottom)
    

    There’s room for refinement, obviously (trigger redraws on padding change, sort out row heights, text padding, etc) but something like this should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.