DataGridView图像只显示“System.Drawing.Bitmap”

时间:2016-05-26 16:26:06

标签: .net vb.net image datagridview

我正在尝试将图片加载到DataGridView中,但是所有单元格显示都是“System Drawn Bitmap”,你可以帮忙吗?

    DataGridPhoto.Columns.Clear()
    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

    Next

我猜我必须告诉单元格它是一张图片,但是找不到怎么做。

此致 格雷厄姆

1 个答案:

答案 0 :(得分:3)

有几种不同类型的DGV列(DataGridViewComboBoxColumnDataGridViewCheckBoxColumn等)。您的代码正在添加无法显示图片的默认DataGridViewTextBoxColumn

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

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

或者:

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

您可能还希望将ImageLayout属性设置为自动调整每个/所有图像的大小。我不确定为什么要清除列,除非它是定义一个完全不同的列布局。要删除任何旧数据,您只需清除行。