Search code examples
vb.netdatagridviewpictureboxvb.net-2010

how multi images appear in the Picture box with the file name in the datagridview


I want to appear multi or a single image in the picturebox with the file name in the datagridview when clicked on datagridview.

for information I use visual studio 2010

Imports System.Data.OleDb

Public Class Form1
  Dim Path As String = "C:\Users\Administrator\Desktop\DBF"
    Dim Pathimage As String = "C:\Users\Administrator\Desktop\CATALOG"
    Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    Private Sub PopulateDataGridView()
        Try
            Dim dt = New DataTable()
            Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4 FROM ITEM"

            Using adapter As New OleDbDataAdapter(query, cn.ToString)
                adapter.Fill(dt)
            End Using

            Me.DataGridView1.DataSource = dt
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PopulateDataGridView()
    End Sub
End Class

RESULT DATAGRIDVIEW WITH FILENAME


Solution

  • Add a FlowLayoutPanel to your form, and paste this handler for DataGridView1 SelectionChanged

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        Dim row = DirectCast(DataGridView1.CurrentRow.DataBoundItem, DataRowView)
        Dim pbAdder =
            Sub(name As String)
                If row(name) IsNot Nothing AndAlso Not String.IsNullOrEmpty(row(name).ToString()) Then
                    FlowLayoutPanel1.Controls.Add(New PictureBox With {.Name = name, .ImageLocation = row(name).ToString()})
                End If
            End Sub
        FlowLayoutPanel1.Controls.Clear()
        pbAdder("FILENAME1")
        pbAdder("FILENAME2")
        pbAdder("FILENAME3")
        pbAdder("FILENAME4")
    End Sub