Results 1 to 10 of 10

Thread: Display Image From dataGridView to multi picturebox in VB.NET

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Display Image From dataGridView to multi picturebox in VB.NET

    Dear All Master,



    I want the image to appear in the multi picturebox (Picturebox1-Picturebox6) when clicked on the "ITC" column in the datagridview.

    The image location is in the "path" column in the join with the "filename1-filename6" column.

    and for those without an image, it appears in the picturebox "no photo".
    I don't want to use databindings because it makes it slow and my record is fifty thousand records.

    for the record i use visual studio 2010
    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
        Private WithEvents dt As New DataTable
        Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
        Private Sub FillDataGridView()
            Try
                'Dim dt = New DataTable()
                dt = New DataTable
                Dim query = "select ITM,ITC,QOH,PRS,FILENAME1,FILENAME2,FILENAME3,FILENAME4,FILENAME5,FILENAME6,PATH FROM ITEM"
    
                Using adapter As New OleDbDataAdapter(query, cn.ToString)
                    adapter.Fill(dt)
                End Using
                Me.DataGridView1.DataSource = dt
    
                'For x As Integer = 4 To 10
                '    Me.DataGridView1.Columns(x).Visible = False
                'Next
            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
            FillDataGridView()
        End Sub
    End Class
    thanks
    roy88
    Attached Images Attached Images  
    Last edited by roy88; Jan 13th, 2022 at 04:02 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    You should bind the DataTable to the DataGridView via a BindingSource. You can then handle the CurrentChanged event of that BindingSource and get the current record from the Current property. You can then build all the file paths and set them in the appropriate PictureBoxes:
    vb.net Code:
    1. Private Sub BindingSource1_CurrentChanged(...) Handles BindingSource1.CurrentChanged
    2.     Dim currentRow = DirectCast(BindingSource1, DataRowView)
    3.     Dim folderPath = CStr(currentRow("PATH"))
    4.  
    5.     PictureBox1.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME1")))
    6.     PictureBox2.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME2")))
    7.     PictureBox3.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME3")))
    8.     PictureBox4.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME4")))
    9.     PictureBox5.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME5")))
    10.     PictureBox6.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME6")))
    11. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    You should bind the DataTable to the DataGridView via a BindingSource. You can then handle the CurrentChanged event of that BindingSource and get the current record from the Current property. You can then build all the file paths and set them in the appropriate PictureBoxes:
    vb.net Code:
    1. Private Sub BindingSource1_CurrentChanged(...) Handles BindingSource1.CurrentChanged
    2.     Dim currentRow = DirectCast(BindingSource1, DataRowView)
    3.     Dim folderPath = CStr(currentRow("PATH"))
    4.  
    5.     PictureBox1.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME1")))
    6.     PictureBox2.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME2")))
    7.     PictureBox3.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME3")))
    8.     PictureBox4.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME4")))
    9.     PictureBox5.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME5")))
    10.     PictureBox6.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME6")))
    11. End Sub
    Dear Mr. jmcilhinney
    Thanks for your reply. Previously I used databindings with only a filename benchmark in datagridview it makes it very slow because the data record was fifty thousand. code from you whether you can appear the image just by clicking in the "ITC" field only. From your code there is an error as follows
    Code:
    Private Sub BindingSource1_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource1.CurrentChanged
            Dim currentRow = DirectCast(BindingSource1, DataRowView) 'Error Value of type 'System.Windows.Forms.BindingSource' cannot be converted to 'System.Data.DataRowView'
            Dim folderPath = CStr(currentRow("PATH"))
    
            PictureBox1.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME1"))) 'Error Combine' is not a member of 'String'
            PictureBox2.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME2"))) 'Error Combine' is not a member of 'String'
            PictureBox3.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME3"))) 'Error Combine' is not a member of 'String'
            PictureBox4.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME4"))) 'Error Combine' is not a member of 'String'
            PictureBox5.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME5"))) 'Error Combine' is not a member of 'String'
            PictureBox6.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME6"))) 'Error Combine' is not a member of 'String'
        End Sub
    Last edited by roy88; Jan 12th, 2022 at 09:56 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    Quote Originally Posted by roy88 View Post
    From your code there is an error as follows
    Code:
    Private Sub BindingSource1_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource1.CurrentChanged
            Dim currentRow = DirectCast(BindingSource1, DataRowView) 'Error Value of type 'System.Windows.Forms.BindingSource' cannot be converted to 'System.Data.DataRowView'
            Dim folderPath = CStr(currentRow("PATH"))
    
            PictureBox1.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME1"))) 'Error Combine' is not a member of 'String'
            PictureBox2.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME2"))) 'Error Combine' is not a member of 'String'
            PictureBox3.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME3"))) 'Error Combine' is not a member of 'String'
            PictureBox4.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME4"))) 'Error Combine' is not a member of 'String'
            PictureBox5.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME5"))) 'Error Combine' is not a member of 'String'
            PictureBox6.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME6"))) 'Error Combine' is not a member of 'String'
        End Sub
    My code is fine. That simply means that you have a String variable named Path at a higher level than that method, while my code was referring to the System.IO.Path class. Notice how I use a more descriptive name for my variable: folderPath. Either change the name of your variable or qualify the Path class name to disambiguate it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    Quote Originally Posted by jmcilhinney View Post
    My code is fine. That simply means that you have a String variable named Path at a higher level than that method, while my code was referring to the System.IO.Path class. Notice how I use a more descriptive name for my variable: folderPath. Either change the name of your variable or qualify the Path class name to disambiguate it.
    Dear Mr. jmcilhinney ,
    Your code is still an error
    Thanks
    roy88

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    No it isn't. You just used it wrong.

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    maybe the problem comes from a question of version of VB and/or frameworks ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    IO.Path.... etc

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    Quote Originally Posted by .paul. View Post
    IO.Path.... etc
    Dear Mr. Paul
    the error is as follows :
    Dim currentRow = DirectCast(BindingSource1, DataRowView) 'Value of type 'System.Windows.Forms.BindingSource' cannot be converted to 'System.Data.DataRowView'
    PictureBox1.ImageLocation = Path.Combine(folderPath, CStr(currentRow("FILENAME1"))) 'Error 'Combine' is not a member of 'String'.
    Thanks
    Roy88

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Display Image From dataGridView to multi picturebox in VB.NET

    The first one is because it should have been BindingSource1.Current, which I did actually mending in my post. If you had pointed out what the error was in the first place, instead of just that there was an error, then we could have cleared that up immediately. The second one has already been explained so fix it as you were instructed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width