Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Database contain 3 field
i.e FieldName    datatype
    Empid         ShortText
    Empname       ShortTExt
    img           OLEObject

and the code in vb.net are

VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim dvgTxtcolumn As New DataGridViewTextBoxColumn
        dvgTxtcolumn.HeaderText = "Roll No"

        Dim dvgTxtcolumn1 As New DataGridViewTextBoxColumn
        dvgTxtcolumn1.HeaderText = "Stu Name"

        Dim dvgImagecolumn As New DataGridViewImageColumn
        dvgImagecolumn.HeaderText = "Image"
        dvgImagecolumn.ImageLayout = DataGridViewImageCellLayout.Stretch


        DataGridView1.Columns.Add(dvgTxtcolumn)
        DataGridView1.Columns.Add(dvgTxtcolumn1)
        DataGridView1.Columns.Add(dvgImagecolumn)

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill
        DataGridView1.RowTemplate.Height = 120
        DataGridView1.AllowUserToAddRows = False
        insert_pic()

    End Sub



VB
Private Sub insert_pic()
        Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\CMS_SOFTWARE\CMS\CCMS\project.mdb")
            conn.Open()
            query = "Select * FROM test"
            Dim adapter2 As New OleDbDataAdapter(query, conn)
            Dim DT2 As New DataTable
            adapter2.Fill(DT2)
For I = 0 To DT2.Rows.Count - 1
Dim ms As New MemoryStream
PictureBox1.Image = DT2.Rows(I)(2)
PictureBox1.Image.Save(MS, PictureBox1.Image.RawFormat)
Dim dtimage As Byte()
 dtimage = ms.ToArray()
DataGridView1.Rows.Add(DT2.Rows(I)(0), DT2.Rows(I)(1), dtimage)

            Next

        Catch ex As Exception
            MsgBox(Err.Description, MsgBoxStyle.Critical)
        End Try
    End Sub


What I have tried:

------------------------------------------
But The program was giving the error is  "Unable to cast object of type 'System.Byte[]
to type 'System.Drawing.Image'


Can anybody pls help me how to solve the problem and how to give the code to display the image in datagrid view from ms access.I am trying this since from last 3 to 4 days but cannot i do it.anybody pls help me



Pls help me how to solve the problem
Posted
Updated 14-Feb-19 10:48am
v2

1 solution

Try:
VB.NET
Dim converter As New ImageConverter()
For I = 0 To DT2.Rows.Count - 1
    Dim dtimage As Image = CType(converter.ConvertFrom(DT2.Rows(I)(2)), Image)
    DataGridView1.Rows.Add(DT2.Rows(I)(0), DT2.Rows(I)(1), dtimage)
Next
NB: Make sure you have Imports System.Drawing at the top of your code file.

You'll also want to change your query to explicitly specify the columns you want, rather than using SELECT * FROM ....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900