簡體   English   中英

錯誤! 無法將類型為“ system.object”的對象轉換為類型為“ system.byte []”的對象

[英]Error! unable to cast object of type 'system.object ' to type 'system.byte[]'

我正在嘗試通過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

您是否嘗試過使用BinaryFormatter,請在MSDN上查看: https ://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx?cs-save-lang =1& cs-lang = VB#代碼片斷-1

您將必須將該對象序列化為MemoryStream並在流上調用ToArray方法以將其轉換為字節數組,並希望能夠通過Image成員對其進行分配。 很抱歉缺少代碼,我的VB語法技能落后了幾十年。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM