Graham Jones Graham Jones - 1 year ago 174 Question

DataGridView image just displays "System.Drawing.Bitmap"

I am trying to load a picture into a DataGridView, but all the cell displays is "System Drawn Bitmap" can you help?

DataGridPhoto.Columns.Add("Name", "Name")
DataGridPhoto.Columns.Add("Form", "Form")
DataGridPhoto.Columns.Add("Admision", "Addmision")
DataGridPhoto.Columns.Add("Photo", "Photo")

DataGridPhoto.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
DataGridPhoto.Columns(0).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridPhoto.Columns(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridPhoto.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridPhoto.Columns(3).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridPhoto.Columns(0).Width = 150
DataGridPhoto.Columns(1).Width = 50
DataGridPhoto.Columns(2).Width = 50
DataGridPhoto.Columns(3).Width = 100

butPrint.Enabled = True
For nLoop = 1 To NoOfLabels
Dim n As Integer = DataGridPhoto.Rows.Add()
DataGridPhoto.Rows(n).Cells("Name").Value = PrnStudentDetails(nLoop, 1) + " " + PrnStudentDetails(nLoop, 2)

DataGridPhoto.Rows(n).Cells("Form").Value = PrnStudentDetails(nLoop, 3)
DataGridPhoto.Rows(n).Cells("Admision").Value = PrnStudentDetails(nLoop, 4)
Dim cPictureFilename As String
cPictureFilename = AppPath & "\Photographs\" & PrnStudentDetails(nLoop, 4) & ".jpg"
If File.Exists(cPictureFilename) Then
DataGridPhoto.Rows(n).Cells("Photo").Value = Image.FromFile(cPictureFilename)
End If


I'm guessing I have to tell the cell it's a picture, but can't find how to do this.


Answer Source

There are several different types of DGV columns (DataGridViewComboBoxColumn, DataGridViewCheckBoxColumn etc) . Your code is adding the default DataGridViewTextBoxColumn which will not be able to display an image.

Dim dc As New DataGridViewImageColumn() With {.HeaderText = "Photo", .Name = "Photo"}

dgv2.Rows(0).Cells(0).Value = Image.FromFile("C:\Temp\London.jpg")


dgv2.Columns.Add(New DataGridViewImageColumn() With {.HeaderText = "Photo",
                                                     .Name = "Photo"})

You may also want to set the ImageLayout property to automatically resized each/all the images. I am not sure why you clear the columns unless it is to define a completely different column layout. To remove any old data you could just clear the rows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download