7 Replies - 1010 Views - Last Post: 23 September 2014 - 01:10 PM Rate Topic: -----

#1 ebolisa   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 22-September 09

Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 03:25 AM

Hi,

Id like to display image data and the image itself in a datagridview based on the image file path stored in a database but I cannot seem to get it right and Id appreciate some guidance.

TIA

The following working code fills a datagridview with images information. The last column (4) holds the full path where the images are stored containing the folder name, the image name and its extension (c:\img\myimage1.jpg).

Private Sub fillDgw()
        Dim db As New DB
        Dim conn = db.MySqlConnection
        Dim sql As String = "Select * from imgtable;"
        Dim cmd As New MySqlCommand
        Dim da As New MySqlDataAdapter
        Dim tbl As New DataTable
        'set connection
        With cmd
            .CommandText = sql
            .Connection = conn
        End With
        'fill table
        With da
            .SelectCommand = cmd
            .Fill(tbl)
        End With
        'fill dgw
        Me.dgwImg.Rows.Clear()
        Me.txbStatus.AppendText("Total Images found " + (tbl.Rows.Count).ToString)
        Me.pb1.Value = 0
        Me.pb1.Minimum = 0
        Me.pb1.Maximum = tbl.Rows.Count

        For i As Integer = 0 To tbl.Rows.Count - 1
            With Me.dgwImg
                .Rows.Add(tbl.Rows(i)("Variant Barcode"), _
                          tbl.Rows(i)("Name"), _
                          tbl.Rows(i)("SizeKB"), _
                          tbl.Rows(i)("LastAccessed"), _
                          tbl.Rows(i)("FullPath"))
                Me.pb1.Value = i 'increment progress bar 
            End With
        Next
    End Sub



The following working code is used to read the images and here where I fail to combine the two routines.

 Private Sub getImages()

        Dim di As New IO.DirectoryInfo(folderPath)
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.jpg")
        Dim fi As IO.FileInfo
    
        Try
            For Each fi In aryFi
                strFullPath = fi.FullName
'do some
            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Fill a DGW with images based on a filepath stored in a DB

#2 thecoat   User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 09:32 AM

DataGridView has a column type DataGridViewImageColumn which will accept images, you'll need one of these to display the images.

If you have a fully qualified file path when you go to add the image to the row you can use the Image.FromFile(<filepath>) method.

Quick example to demonstrate the basic premise:
        DataGridView1.Columns.Add(New DataGridViewImageColumn(False)) 'This could be done at design time or runtime.
        For Each f As String In System.IO.Directory.GetFiles("c:\ImagePath\")
            Try 'Wrap this in a try catch in case the image path contains files that aren't images.
                DataGridView1.Rows.Add(New Object() {Image.FromFile(f)})
            Catch ex As Exception
            End Try
        Next


Was This Post Helpful? 0
  • +
  • -

#3 ebolisa   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 160
  • Joined: 22-September 09

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 09:44 AM

Thank you.

I did try that option before but VS2010 won’t accept Image.FromFile option as it underlines it with the msg: 'FromFile' is not a member of System.Windows.Forms.DataGridViewImageColumn.

EDIT: Solved with this line:

 Me.dgwImg("Image", i).Value = System.Drawing.Image.FromFile(tbl.Rows(i)("FullPath").ToString)


This post has been edited by ebolisa: 23 September 2014 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 11:21 AM

Hi,
I am trying to figure out if there is any kind of a pattern you are following here to keep the correct image in the same row that the info for it is in.

If you are loading your image file names and info into a datatable and showing that info in the DataGridView and then just looping through all the images in a folder and adding them to 1 column, the images might not be added to the same row that has the correct pathname and info for it. Especially if any images are added or removed from the folder.

Does it mater which row the images are added to?

If it does, it seems to me you would need to fill the data into the DataGridView and then loop through each row to get the full pathname and then load that image and add it to the cell for the images.

I don`t know much about using databases or datatables so, there may be a better way to do this, i don`t know.

This post has been edited by IronRazer: 23 September 2014 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 12:03 PM

Here is a basic example of what i meant. However, now that i have done this to loop through the DataGridView rows to get the full pathname from the 1st column and then set the 2nd column to the image i was thinking maybe you can just loop through your datatable and get the pathnames from there.

Maybe you can modify this example to work for you or perhaps someone with more experience with databases, datatables, and datagridviews will have a much better suggestion than this. As i said, i don`t know much about databases or datatables. :)

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'This just adds the full pathnames to the 1st column in the DataGridView that is a DataGridViewTextBoxColumn type
        'I just used this to add the pathnames instead of a datatable to make it easy on myself.
        Dim imgpath As String = "C:\TestFolder\Image Folder"
        For Each img As String In IO.Directory.GetFiles(imgpath, "*.png")
            DataGridView1.Rows.Add(img)
        Next

        'call the sub to load the correct images for the correct rows
        GetImages()
    End Sub

    Private Sub GetImages()

        'loop through all the rows of the DataGridView
        For ri As Integer = 0 To DataGridView1.Rows.Count - 1

            'Make sure it is not a new row
            If Not DataGridView1.Rows(ri).IsNewRow Then

                'get the image file pathname
                Dim fn As String = DataGridView1.Rows(ri).Cells(0).Value.ToString

                'Make sure the file exists
                If IO.File.Exists(fn) Then

                    'Add the image to the 2nd column that is a DataGridViewImageColumn
                    DataGridView1.Rows(ri).Cells(1).Value = Image.FromFile(fn)
                End If
            End If
        Next
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#6 thecoat   User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 12:29 PM

IronRazer I think you took my post as being part of the OP's project. I just posted something that could be easily used to demonstrate the principal behind what they needed to do, not the direct solution for them. Their line of code that was the solution for them appears to use the loop variable that they are using to add the records to the datagridview so it should all be in sync.

It appears that the path to the file is a string in their database and so when they loop through the table records they can use that field as a file pointer and add the loaded image as they populate each row of the gridview.

This post has been edited by thecoat: 23 September 2014 - 12:29 PM

Was This Post Helpful? 1
  • +
  • -

#7 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,502
  • Joined: 12-December 12

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 12:31 PM

Slightly off topic, but in WPF I think it would be possible to use a ValueConverter to automatically associate the image path to the image itself, and then bind to the image. SO topic. It may work without rolling a ValueConverter.

This isn't of much help though ;) as I'm not sure how this would be achieved in WinForms (presumably requiring an ORM, if possible at all).

This post has been edited by andrewsw: 23 September 2014 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Fill a DGW with images based on a filepath stored in a DB

Posted 23 September 2014 - 01:10 PM

@ thecoat,
No, actually i was looking at OP`s 2nd code block and thought they where trying to use that to load the images separately for some reason. That is why i had the thought that the images may not be added to the correct row as the info for the images.

Now that i look again it looks like he is just looping through the DataTable at the end of the first code block and adding the info including the Full Path. I guess i should have looked a little harder because, i guess you can just add the image to the Image Column in that same loop being the Full Path is rite there in the DataTable. :)

This post has been edited by IronRazer: 23 September 2014 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1