Displaying image in datagrid from URL stored in database

Posted on 2007-11-14
Last Modified: 2013-11-26

The problem is pretty much as the title suggests. This might be fairly easy but I am just starting out with 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?

Question by:jkdt0077
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Expert Comment

ID: 20284413
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"

...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


Expert Comment

ID: 20284432
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")

Accepted Solution

JumpsInLava earned 250 total points
ID: 20284500
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

Author Comment

ID: 20287677

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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question