add image to datagridview

Kim 41 Reputation points


I have an app in with a database.
Now in the datagridview column "photo" I want to display an image according to the path which is in another cell "photopath".
I have the following code from an old app but I can't get it to work.

 If Me.FietsdragerBindingSource.Position > -1 Then
            For Each NRow In Me.FietsdragerDataGridViewX.Rows
                If NRow.Cells(2).Value IsNot DBNull.Value Then
                        filenaam = (NRow.Cells(2).Value())
                        NRow.Cells(1).value() = Image.FromFile(Settings.image.Text & filenaam & ".jpg")
                    Catch ex As Exception
                        NRow.Cells(1).value() = My.Resources._error
                    End Try
                    'If fotopadTextBox.Text = Nothing Then
                    '    FotoPictureBox.Image = My.Resources._error
                    'End If
                End If
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,083 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,091 Reputation points

    Simple example here my help. There are a couple of hard coded paths - you would need to edit accordingly if you want to try the example. You can drag the cell edges to resize.


    Option Strict On  
    Option Explicit On  
    Public Class Form1  
     Dim dt As New DataTable("Freddy")  
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
     With dt  
     .Columns.Add("ID", GetType(Integer))  
     .Columns.Add("Image", GetType(Image))  
     .Columns.Add("Desc", GetType(String))  
     .Rows.Add(123, Image.FromFile("C:\Users\lesha\Documents\VB Resources\Images\ABS.jpg"), "Some words")  
     .Rows.Add(321, Image.FromFile("C:\Users\lesha\Documents\VB Resources\Images\Amedee Lighthouse.jpg"), "More words")  
     End With  
     With DataGridView1  
     .DataSource = dt  
     .AllowUserToAddRows = False  
     End With  
     End Sub  
     Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting  
     If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return  
     If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Image) Then  
     DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom  
     End If  
     End Sub  
    End Class  
    0 comments No comments

0 additional answers

Sort by: Most helpful