none
jpg not showing in datagridview column RRS feed

  • Question

  • Found this for showing a jpg in a datagridview column.

    But it is only showing a X ?

    I tryed BMP , PNG but it is still the same , the column is there but not the picture.

       Dim img As New DataGridViewImageColumn()
            Dim inImg As Image = Image.FromFile("D:\PPH.jpg")
            img.Image = inImg
            DataGridView2.Columns.Add(img)
            img.HeaderText = "Image"
            img.Name = "img"

    What am i missing ?

    Saturday, July 28, 2018 12:01 PM

Answers

  • Found it,

    It is showing when there is data in the datagridview.

    Thanks for the reply's 

       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
    
            Dim img As New DataGridViewImageColumn()
            Dim inImg As Image = Image.FromFile("D:\red.bmp")
            img.Image = inImg
            DataGridView1.Columns.Add(img)
            img.HeaderText = "Image"
            img.Name = "img"
    
        End Sub


    • Marked as answer by KeesBlunder Saturday, July 28, 2018 7:06 PM
    • Edited by KeesBlunder Saturday, July 28, 2018 7:07 PM
    Saturday, July 28, 2018 7:06 PM

All replies

  • When you setup the DGV you must create a special imagecolumn as shown in the example at the end of this page CreateColumns sub routine.

    Saturday, July 28, 2018 12:37 PM
  • Here is an example

    Imports System.IO
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.Columns.Add(New DataGridViewImageColumn With
            {
                .Name = "ImageCol",
                .HeaderText = "Picture"
            })
    
            Dim apples As String() = Directory.
                    GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.png")
    
            For Each file As String In apples
                DataGridView1.Rows.Add(New Object() {Image.FromFile(file)})
            Next
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, July 28, 2018 1:21 PM
    Moderator
  • Found it,

    It is showing when there is data in the datagridview.

    Thanks for the reply's 

       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
    
            Dim img As New DataGridViewImageColumn()
            Dim inImg As Image = Image.FromFile("D:\red.bmp")
            img.Image = inImg
            DataGridView1.Columns.Add(img)
            img.HeaderText = "Image"
            img.Name = "img"
    
        End Sub


    • Marked as answer by KeesBlunder Saturday, July 28, 2018 7:06 PM
    • Edited by KeesBlunder Saturday, July 28, 2018 7:07 PM
    Saturday, July 28, 2018 7:06 PM