add picture in datagridview

Kim 41 Reputation points
2022-04-06T19:03:22.893+00:00

hallo,

I have linked a winform with a data grid view to an mdf database.
via a button where I select the file I load a photo into a picturebox.

Private Sub btn_foto_Click(sender As Object, e As EventArgs) Handles btn_foto.Click    
        Dim dialog As New OpenFileDialog()    
        If DialogResult.OK = dialog.ShowDialog Then    
            FotopadTextBox.Text = dialog.FileName    
        End If    

with next button I load the photos in the datagridview but all cells get the same photo instead of the corresponding photo as stated in (fotoPadTextbox)

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
If Me.FietsdragersBindingSource.Position > -1 Then
For Each NRow In Me.FietsdragersDataGridView.Rows
If NRow.Cells(6).Value IsNot DBNull.Value Then
Try
filenaam = (NRow.Cells(6).Value())
NRow.Cells(0).value() = Image.FromFile(FotopadTextBox.Text)
Catch ex As Exception
NRow.Cells(0).value() = My.Resources.x
End Try
End If
Next
End If
End Sub

Can someone help me with what I'm doing wrong.
thx

190644-knipsel1.jpg

Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2022-04-07T05:28:27.997+00:00

    Hi @Kim ,

    filenaam = (NRow.Cells(6).Value())  
    NRow.Cells(0).value() = Image.FromFile(FotopadTextBox.Text)  
    

    In the loop of your code, the value of FotopadTextBox.Text is used for each row of images added, not NRow.Cells(6).Value() of the corresponding row, and the images are the same for all rows.
    So you need to modify the above code to get the correct value.

    NRow.Cells(0).value() = Image.FromFile(NRow.Cells(6).Value())  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 125.6K Reputation points
    2022-04-06T19:25:56.13+00:00

    Try replacing

    NRow.Cells(0).value( ) = Image.FromFile(FotopadTextBox.Text)

    with

    NRow.Cells(0).value = filenaam

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.