我正在尝试通过CellClick事件从DataGridView访问数据。在 VB .NET
中当我点击单元格时,我从表格中获得所有数据但不能在 PictureBox 中查看图像。 当我只尝试此代码时:
pic1 = selectedRow.Cells(13).Value
Dim ms As New MemoryStream(pic1)
pb_stud_pic.Image = Image.FromStream(ms)
我有错误!无法转换类型为'system.object'的对象输入' system.byte []'
然后我尝试下面的代码:但后来我没有在图片框中获取图像..
我尝试过:
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
答案 0 :(得分:0)
您是否尝试过使用BinaryFormatter,请参阅MSDN: https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
您必须将对象序列化为MemoryStream并在流上调用ToArray方法将其转换为字节数组,并希望能够将其分配给Image成员。对于缺少代码感到抱歉,我的VB语法技能落后数十年。