vb. net datagridview отображает system.byte [] - PullRequest
0 голосов
/ 06 августа 2020

enter image description here

I use datagridview to display images from database but first I used datatable and i flipped it to show the data horizontally and pass it to datagridview so I need some help to fix my code or a way to display the data horizontally on datagridview. Please I have been trying for long time and did alot of search but nothing

Public Sub getProdauct()
    Try
        Dim dt_image As New DataTable
        Dim dt As New DataTable
        Dim converter As New ImageConverter
        dt.Columns.Add("picture", System.Type.GetType("System.Byte[]"))
        sqL = "SELECT ProductNo, ProductCOde, P.Description, Barcode, UnitPrice, StocksOnHand, ReorderLevel, CategoryName, picture, filename, filesize FROM Product as P, Category as C WHERE C.CategoryNo = P.CategoryNo"
        ConnDB()
        cmd = New MySqlCommand(sqL, conn)
        da.SelectCommand = cmd
        da.Fill(dt_image)
        'dt_image = Transpose(dt_image)
        For Each dr As DataRow In dt_image.Rows
            Dim image_buffer = CType(dr("picture"), Byte())
            Dim image_strem As New MemoryStream(image_buffer, True)
            image_strem.Write(image_buffer, 0, image_buffer.Length)
            Dim R As DataRow = dt.NewRow
            R("picture") = converter.ConvertTo(ResizeImage(New Bitmap(image_strem), 120, 120), GetType(Byte()))
            dt.Rows.Add(R)
            'DataGridViewp.Rows.Add(ResizeImage(New Bitmap(image_strem), 120, 120))
            image_strem.Close()
        Next
        DataGridViewp.DataSource = Transpose(dt)
    Catch ex As Exception
        MsgBox(ex.ToString)
    Finally
        cmd.Dispose()
        conn.Close()
    End Try
End Sub

Private Function Transpose(ByVal table As DataTable) As DataTable
    Dim flippedTable As New DataTable
    'creates as many columns as rows in source table
    flippedTable.Columns.AddRange(
        table.Select.Select(
            Function(dr) New DataColumn("col" & table.Rows.IndexOf(dr), GetType(Object))
            ).ToArray)
    'iterates columns in source table
    For Each dc As DataColumn In table.Columns
        'get array of values of column in each row and add as new row in target table
        flippedTable.Rows.Add(table.Select.Select(Function(dr) dr(dc)).ToArray)
    Next
    Return flippedTable
End Function

Private Function ResizeImage(ByVal img As Image, ByVal w As Integer, ByVal h As Integer) As Image
    Dim newImage As New Bitmap(w, h)
    Using g As Graphics = Graphics.FromImage(newImage)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(img, New Rectangle(0, 0, w, h))
    End Using
    Return newImage
End Function
...