Link to home
Start Free TrialLog in
Avatar of jkdt0077
jkdt0077Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of JumpsInLava
JumpsInLava
Flag of United States of America image

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


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")
ASKER CERTIFIED SOLUTION
Avatar of JumpsInLava
JumpsInLava
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkdt0077

ASKER

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