Click here to Skip to main content
13,799,517 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hey Friends. I Am Trying To Access Data From DataGridView To Form By CellClick Event. In VB .NET
When Ever I Click On Cell I Got All Data From Table To Form But Not Able To See Image In PictureBox.

When I Tried Only This Code:
pic1 = selectedRow.Cells(13).Value
Dim ms As New MemoryStream(pic1)
pb_stud_pic.Image = Image.FromStream(ms)
I Got Error! unable to cast object of type 'system.object ' to type 'system.byte[]'

Then I Try Below Code:
but Then I Dont Get Image in picture Box..

What I have tried:

Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

            Dim index As Integer = e.RowIndex
            Dim selectedRow As New DataGridViewRow()
            selectedRow = DataGridView1.Rows(index)

            txt_stud_no.Text = selectedRow.Cells(0).Value.ToString()
            txt_stud_name.Text = selectedRow.Cells(1).Value.ToString()
            dtp_stud_dob.Value = selectedRow.Cells(2).Value.ToString()
            txt_stud_gender.Text = selectedRow.Cells(3).Value.ToString()

            If txt_stud_gender.Text = "Male" Then
                rdo_male.Checked = True
                rdo_female.Checked = True
            End If

            cb_stud_blood.Text = selectedRow.Cells(4).Value.ToString()
            txt_stud_add.Text = selectedRow.Cells(5).Value.ToString()
            txt_stud_city.Text = selectedRow.Cells(6).Value.ToString()
            txt_stud_pc.Text = selectedRow.Cells(7).Value.ToString()
            txt_stud_mob.Text = selectedRow.Cells(8).Value.ToString()
            cb_adyear.Text = selectedRow.Cells(9).Value.ToString()
            cb_cbranch.Text = selectedRow.Cells(10).Value.ToString()
            txt_stud_email.Text = selectedRow.Cells(11).Value.ToString()
            cb_csem.Text = selectedRow.Cells(12).Value.ToString()

            dr = cm.ExecuteReader()
            If dr.Read() Then
                If Not dr.IsDBNull(dr.GetOrdinal("Image")) Then
                    pic1 = CType(dr("Image"), Byte())
                    pic1 = selectedRow.Cells(13).Value
                    pic1 = ds.Tables("Student").Rows(i)(13)

                    If Not (pic1 Is Nothing) Then
                        Dim ms As New MemoryStream(pic1)
                        pb_stud_pic.Image = Image.FromStream(ms)
                        pb_stud_pic.Image = Nothing
                    End If
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Error!" & ex.Message)
        End Try
    End Sub
Posted 4-Mar-17 3:44am
Updated 4-Mar-17 3:58am

1 solution

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

Solution 1

Member 13037760 4-Mar-17 9:00am
Will You Tell me In Detail Please.. New In VB .NET
Graeme_Grant 4-Mar-17 9:12am
result = Convert.ToByte(value)

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
Web04 | 2.8.181207.3 | Last Updated 4 Mar 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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