none
RETERIVE IMAGE FROM DATAGRIDVIEW RRS feed

  • Question

  • HI

    Use the following code to RETERIVE the image from the cell to the PICTUREBOX
    The problem is that if there is no image in the cell, the last images that have been found will still be found in the
    PICTUREBOX
    I do not want to have a picture of the
    PICTUREBOXin case the cell is empty of images

    Private Sub DATAGRID_SERVER_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DATAGRID_SERVER.CellEnter
            Dim img As Byte()
            Try
                img = DATAGRID_SERVER.CurrentRow.Cells(7).Value
                Dim ms As New MemoryStream(img)
                PictureBox1.Image = Image.FromStream(ms)
    
            Catch ex As Exception
    
            End Try
        End Sub


    • Edited by ahmeddc Monday, October 1, 2018 6:41 AM
    Monday, October 1, 2018 6:39 AM

Answers

  • I changed the code and nothing happens

     Dim img As Byte()
            Try
                If DATAGRID_SERVER.CurrentRow.Cells(7).Value = Nothing Then
                    PictureBox1.Image = Nothing
                    Exit Sub
                Else
                    img = DATAGRID_SERVER.CurrentRow.Cells(7).Value
                    Dim ms As New MemoryStream(img)
                    PictureBox1.Image = Image.FromStream(ms)
                End If
    
    
            Catch ex As Exception
    
            End Try

    i wasnt aware a value could be nothing. I am thinking you need to check the object itself if it IS nothing not = to nothing. The value, however, maybe = to DBNull.Value

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by ahmeddc Monday, October 1, 2018 12:47 PM
    Monday, October 1, 2018 12:16 PM

All replies

  • Then do PictureBox1.Image = Nothing if the cell Value is Nothing. Also, I'd suggest to not use an empty Catch.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 1, 2018 9:08 AM
    Moderator
  • I changed the code and nothing happens

     Dim img As Byte()
            Try
                If DATAGRID_SERVER.CurrentRow.Cells(7).Value = Nothing Then
                    PictureBox1.Image = Nothing
                    Exit Sub
                Else
                    img = DATAGRID_SERVER.CurrentRow.Cells(7).Value
                    Dim ms As New MemoryStream(img)
                    PictureBox1.Image = Image.FromStream(ms)
                End If
    
    
            Catch ex As Exception
    
            End Try

    Monday, October 1, 2018 11:12 AM
  • Well since I can't take the time now to reproduce your situation I would suggest placing a breakpoint on the line checking the value (one line below the tray) by hovering over it and see what the value is for a known image that has no image, once known you know what to check for. Perhaps it's an empty Byte array vs with an image a byte array with a length greater than zero.

    Personally I'd never check the Cell value but check the underlying DataRow field value. That is where an BindingSource makes it easy (and I mentioned using a BindingSource in one of your prior questions) to get data and check data.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 1, 2018 11:23 AM
    Moderator
  • I changed the code and nothing happens

     Dim img As Byte()
            Try
                If DATAGRID_SERVER.CurrentRow.Cells(7).Value = Nothing Then
                    PictureBox1.Image = Nothing
                    Exit Sub
                Else
                    img = DATAGRID_SERVER.CurrentRow.Cells(7).Value
                    Dim ms As New MemoryStream(img)
                    PictureBox1.Image = Image.FromStream(ms)
                End If
    
    
            Catch ex As Exception
    
            End Try

    i wasnt aware a value could be nothing. I am thinking you need to check the object itself if it IS nothing not = to nothing. The value, however, maybe = to DBNull.Value

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by ahmeddc Monday, October 1, 2018 12:47 PM
    Monday, October 1, 2018 12:16 PM
  • hi gtripodi

    i use this code and every thing was good

     If IsDBNull(DATAGRID_SERVER.CurrentRow.Cells(7).Value) = True Then PictureBox1.Image = Nothing : Exit Sub

    Monday, October 1, 2018 12:47 PM