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
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:
Private Sub BindingSource1_CurrentChanged(...) Handles BindingSource1.CurrentChanged
Dim currentRow = DirectCast(BindingSource1, DataRowView)
Re: Display Image From dataGridView to multi picturebox in VB.NET
Originally Posted by jmcilhinney
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:
Private Sub BindingSource1_CurrentChanged(...) Handles BindingSource1.CurrentChanged
Dim currentRow = DirectCast(BindingSource1, DataRowView)
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
Re: Display Image From dataGridView to multi picturebox in VB.NET
Originally Posted by roy88
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.
Re: Display Image From dataGridView to multi picturebox in VB.NET
Originally Posted by jmcilhinney
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
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)
Re: Display Image From dataGridView to multi picturebox in VB.NET
Originally Posted by .paul.
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
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.