• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

Displaying image in datagrid from URL stored in database

Hi

The problem is pretty much as the title suggests. This might be fairly easy but I am just starting out with vb.net. I have a datagridview displaying several rows. In one column is an image url. I would like this image to be displayed for each row as part of the datagrid. Is this possible?

Thanks,
Dan
0
jkdt0077
Asked:
jkdt0077
  • 3
1 Solution
 
JumpsInLavaCommented:
something like this would work (need some tweaking to fit your implementation)...

...add an image column after the data is loaded in the datagridview:

Me.DataGridView1.DataSource = myData
Dim myImageColumn As New System.Windows.Forms.DataGridViewImageColumn
myImageColumn.HeaderText = "MyImage"
myImageColumn.Name = "MyImage"
Me.DataGridView1.Columns.Add(myImageColumn)


...load an image on the cell formatting event
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting


    ' you would probably want to figure out what cell value has your url (based on what row you are on), and
    ' set this here.
    dim imageURL As String = "http:/server/image.gif"


    ' change this to where the image column ends up...or figure out the header name or something to be more dynamic
    Dim colIndex As Integer = 8

    ' if using true URL, create webclient to read the image
    Dim myWebClient As New System.Net.WebClient

    ' load it up
    If e.ColumnIndex = 8 Then e.Value = Image.FromStream(myWebClient.OpenRead(imageURL))

End Sub


0
 
JumpsInLavaCommented:
woops.  declared colIndex, and didn't use it.  oh well, hopefully you get the idea...

oh, and if you want to read it from a file then you could use:
If e.ColumnIndex = colIndex Then e.Value = Image.FromFile("c:\image.gif")
0
 
JumpsInLavaCommented:
hmmm...posted it, and didn't like it.  :-)    this is would probably be more what you would want for the CellFormatting event:

If Me.DataGridView1.Columns(e.ColumnIndex).HeaderText = "MyImage" Then

    Dim imageURL As String = Convert.ToString(Me.DataGridView1.Rows(e.RowIndex).Cells("ImageURL").Value)
   
    Dim myWebClient As New System.Net.WebClient
   
    e.Value = Image.FromStream(myWebClient.OpenRead(imageURL))

End If
0
 
jkdt0077Author Commented:
Hi

Thanks for that, Ive not tried it yet as Im away until this evening. I will try it as soon as I get back and let you know how it goes.

Thanks,
Dan
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now