在Datagridview中显示图像

时间:2018-01-30 05:33:49

标签: vb.net image datagridview

我正在尝试使用ms-access和vb.net 2012在datagridview中显示图像。 我在商店图像路径中的数据库不直接成像,因此当尝试在datagridview中检索图像时,它显示图像路径,而不是图像。

所以我该如何解决?

   Private Sub design_list_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Design_managementDataSet.design_details' table. You can move, or remove it, as needed.
            'Me.Design_detailsTableAdapter.Fill(Me.Design_managementDataSet.design_details)

            cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Design Management\Database\design_management.accdb"

            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            designDatagridShow()
        End Sub

 Private Sub designDatagridShow()
        Dim ds As New DataSet
        Dim dt As New DataTable

        ds.Tables.Add(dt)

        Dim da As New OleDbDataAdapter

        da = New OleDbDataAdapter("select * from design_details", cnn)

        da.Fill(dt)
        design_datagridview.AutoGenerateColumns = False

        Dim ColImage As New DataGridViewImageColumn
        Dim Img As New DataGridViewImageCell

        design_datagridview.ColumnCount = 3

        design_datagridview.Columns(0).Name = "design_number"
        design_datagridview.Columns(0).HeaderText = "Design Number"
        design_datagridview.Columns(0).DataPropertyName = "design_number"

        design_datagridview.Columns(1).Name = "design_collection"
        design_datagridview.Columns(1).HeaderText = "Design Collection"
        design_datagridview.Columns(1).DataPropertyName = "design_collection"

        design_datagridview.Columns(2).Name = "price"
        design_datagridview.Columns(2).HeaderText = "Design Price"
        design_datagridview.Columns(2).DataPropertyName = "price"

        design_datagridview.Columns(3).Name = "design_image"
        design_datagridview.Columns(3).HeaderText = "Design Image"
        design_datagridview.Columns(3).DataPropertyName = "design_image"

        design_datagridview.DataSource = dt
        Dim value As String
        value = design_datagridview.Columns(3).HeaderText = "design_image"
        Label1.Text = value

        cnn.Close()

    End Sub

更新1: enter image description here

感谢。

1 个答案:

答案 0 :(得分:1)

添加另一个System.Drawing.Image数据类型的列,然后使用System.Drawing.Image.FromFile(...)基于图像列的路径设置图像,如下所示:

Dim imageColumn As New DataColumn
imageColumn.ColumnName = "ActualImage"
imageColumn.DataType = GetType(System.Drawing.Image)
dt.Columns.Add(ImageColumn)

For Each row As DataRow in dt.Rows
   row("ActualImage") = System.Drawing.Image.FromFile(row("design_image"))
Next

dt.AcceptChanges()


Dim dgvImageColumn As New DataGridViewImageColumn
dgvImageColumn.DataPropertyName = "ActualImage"
dgvImageColumn.Name = "ActualImage"
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
design_datagridview.Columns.Add(dgvImageColumn)

design_datagridview.DataSource = dt