Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
VB
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:

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

            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
            Else
                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()

            con.Open()
            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)
                    Else
                        pb_stud_pic.Image = Nothing
                    End If
                End If
            End If
            con.Close()

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

1 solution

 
Share this answer
 
Comments
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)



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