Click here to Skip to main content
14,102,169 members
Rate this:
 
Please Sign up or sign in to 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

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



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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try:
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 ....
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 14 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100