none
Add image to combobox of datagridview RRS feed

  • Question

  • Hi:

    I am using VB.net 2019.  I have a unbound datagridview and one of the columns is a combobox.  On my form, I also have an imagelist that has 4 pictures in it.  See the sample images below.  Is there a way to show the pictures in the comboxbox instead of the text (for the datagridview)?

    I know how to show images for a stand alone combobox by using the DrawItem and MesureItem events.  However, I could not see similar events for the datagridview. 

    I would appreciate a sample code.  Thank you.

    Bob

                   

    Sunday, July 7, 2019 2:42 AM

Answers

  • Hi,

    I have no good advice,I think my method is ok.

    Public Class Form1
        Dim Combobox1 As ComboBox
    
        Dim dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    
            dt.Columns.Add("column1")
            dt.Columns.Add("column2")
            dt.Rows.Add(New Object() {1, 2})
            dt.Rows.Add(New Object() {3, 4})
            dt.Rows.Add(New Object() {5, 6})
            DataGridView1.DataSource = dt
    
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "ComboxColumn"})
    
            Dim dgvcolumn As Integer = DataGridView1.Columns.Count
            For i = 0 To DataGridView1.Rows.Count - 2
                Combobox1 = New ComboBox
                With Combobox1
                    .DropDownStyle = ComboBoxStyle.DropDownList
                    .DrawMode = DrawMode.OwnerDrawFixed
                    .DataSource = {"", "", "", ""}
                    .Location = DataGridView1.GetCellDisplayRectangle(dgvcolumn - 1, i, False).Location
                    .Width = DataGridView1.GetCellDisplayRectangle(dgvcolumn - 1, i, False).Width
    
                End With
                AddHandler Combobox1.DrawItem, AddressOf ComboBox1_DrawItem
                DataGridView1.Controls.Add(Combobox1)
            Next
    
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
            'Dim g As Graphics = e.Graphics
            Dim r As Rectangle = e.Bounds
            Dim imageSize As Size = ImageList1.ImageSize
            If e.Index >= 0 Then
                Dim s As String = CStr(Combobox1.Items(e.Index))
                If e.State = (DrawItemState.NoAccelerator Or DrawItemState.NoFocusRect) Then
                    e.Graphics.FillRectangle(New SolidBrush(Color.White), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                Else
                    e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                End If
            End If
        End Sub
    
    End Class
    


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 15, 2019 3:06 AM
    Moderator

All replies

  • Hello,

    99 percent of the time the method to show images in a DataGridView is a DataGridViewImageColumn, for what you want you will need to do a fair amount of coding e.g. (which is not exactly what you want but should spun ideas).

    https://social.msdn.microsoft.com/Forums/windows/en-US/1f41f16d-17a9-4c52-92cf-3de9426a2dd7/image-inside-datagridviewcomboboxcell?forum=winformsdatacontrols


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, July 7, 2019 2:58 AM
    Moderator
  • Hi,

    You can add combobox to Datagridview,I only add one.

    Public Class Form1
        Dim Combobox1 As New ComboBox
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            With Combobox1
                .DropDownStyle = ComboBoxStyle.DropDownList
                .DrawMode = DrawMode.OwnerDrawFixed
                .DataSource = {"", "", "", ""}
                .Location = DataGridView1.GetCellDisplayRectangle(0, 0, False).Location
                .Width = DataGridView1.GetCellDisplayRectangle(0, 0, False).Width
    
            End With
            AddHandler Combobox1.DrawItem, AddressOf ComboBox1_DrawItem
            DataGridView1.Controls.Add(Combobox1)
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
            'Dim g As Graphics = e.Graphics
            Dim r As Rectangle = e.Bounds
            Dim imageSize As Size = ImageList1.ImageSize
            If e.Index >= 0 Then
                Dim s As String = CStr(Combobox1.Items(e.Index))
                If e.State = (DrawItemState.NoAccelerator Or DrawItemState.NoFocusRect) Then
                    e.Graphics.FillRectangle(New SolidBrush(Color.White), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                Else
                    e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                End If
            End If
        End Sub
    
    End Class
    


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, July 7, 2019 7:49 AM
    Moderator
  • Thanks Karen.  I had seen that, but I think it that solution they only draw a filled color rectangle, and not an actual image.  I agree with you, it looks like a lot of work.

    Bob

    Tuesday, July 9, 2019 3:46 AM
  • Thanks Alex.  Your solution is 90% there. 

    bob

    Tuesday, July 9, 2019 5:20 AM
  • Hi,

    If you have anything else about this issue, please feel free to contact us.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 9:17 AM
    Moderator
  • Hi Alex:

    Thanks.  I payed a bit with your code, and it does not change the combobox of the datagridview to display images.  All it does, it puts a brand new comboxbox on top of cell(0,0) that shows the image.  Is there a way to actually have all the comboboxes of the datagridview to show the images? Thanks;

    Bob

    Saturday, July 13, 2019 6:58 AM
  • Hi,

    I have no good advice,I think my method is ok.

    Public Class Form1
        Dim Combobox1 As ComboBox
    
        Dim dt As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    
            dt.Columns.Add("column1")
            dt.Columns.Add("column2")
            dt.Rows.Add(New Object() {1, 2})
            dt.Rows.Add(New Object() {3, 4})
            dt.Rows.Add(New Object() {5, 6})
            DataGridView1.DataSource = dt
    
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "ComboxColumn"})
    
            Dim dgvcolumn As Integer = DataGridView1.Columns.Count
            For i = 0 To DataGridView1.Rows.Count - 2
                Combobox1 = New ComboBox
                With Combobox1
                    .DropDownStyle = ComboBoxStyle.DropDownList
                    .DrawMode = DrawMode.OwnerDrawFixed
                    .DataSource = {"", "", "", ""}
                    .Location = DataGridView1.GetCellDisplayRectangle(dgvcolumn - 1, i, False).Location
                    .Width = DataGridView1.GetCellDisplayRectangle(dgvcolumn - 1, i, False).Width
    
                End With
                AddHandler Combobox1.DrawItem, AddressOf ComboBox1_DrawItem
                DataGridView1.Controls.Add(Combobox1)
            Next
    
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
            'Dim g As Graphics = e.Graphics
            Dim r As Rectangle = e.Bounds
            Dim imageSize As Size = ImageList1.ImageSize
            If e.Index >= 0 Then
                Dim s As String = CStr(Combobox1.Items(e.Index))
                If e.State = (DrawItemState.NoAccelerator Or DrawItemState.NoFocusRect) Then
                    e.Graphics.FillRectangle(New SolidBrush(Color.White), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                Else
                    e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), r)
                    ImageList1.Draw(e.Graphics, r.Left, r.Top, e.Index)
                    e.DrawFocusRectangle()
                End If
            End If
        End Sub
    
    End Class
    


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 15, 2019 3:06 AM
    Moderator