0

I have a list of products from a database in sql server shown in a datagridview.

My goal is to achieve that through a Picturebox I can assign each product an image, this through a button that opens my files to select an image. Once the image is loaded, the save button will perform its task.

Private Sub btnFoto_Click(sender As Object, e As EventArgs) Handles btnFoto.Click
    Dim openfile As New OpenFileDialog()

    openfile.ShowDialog()

    ruta = openfile.FileName

    Try
        PictureBox1.Image = Image.FromFile(ruta)
    Catch ex As Exception
        PictureBox1().Image = Image.FromFile(CurDir() + "\imagen\sin.png")
    End Try
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Try
        PictureBox1.Image = Image.FromFile(ruta)
    Catch ex As Exception
        PictureBox1().Image = Image.FromFile(CurDir() + "\imagen\sin.png")
    End Try
End Sub

Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click
    Try
        My.Computer.FileSystem.CopyFile(ruta, CurDir() + "\imagen\" + ".jpg")

    Catch ex As Exception
        PictureBox1.Image = Image.FromFile(CurDir() + "\imagen\sin.png")
        MsgBox("...")

    End Try
End Sub

Note: the images are inserted however they are changed in all rows and I don't know how to assign one by one

Thank you.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
EriMI
  • 1
  • 1
    How does this line make any sense: `My.Computer.FileSystem.CopyFile(ruta, CurDir() + "\imagen\" + ".jpg")`? Think about the path you're copying to. – jmcilhinney Jan 05 '20 at 02:15
  • Under the `DataGridView1_CellContentClick` event, you can't do `PictureBox1.Image = Image.FromFile(ruta)` because the string `ruta` does not change when the Datagridview cell is clicked. You would have to insert either the picture or the string into the database when saving. Then under that event, you carry out a SELECT statement to fetch the image or string from the database for the particular entry. – preciousbetine Jan 05 '20 at 05:48

0 Answers0