Solved

Displaying image in datagrid from URL stored in database

Posted on 2007-11-14
5
969 Views
Last Modified: 2013-11-26
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
Comment
Question by:jkdt0077
[X]
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
5 Comments
 
LVL 2

Expert Comment

by:JumpsInLava
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"
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
 
LVL 2

Expert Comment

by:JumpsInLava
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")
0
 
LVL 2

Accepted Solution

by:
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
0
 
LVL 7

Author Comment

by:jkdt0077
ID: 20287677
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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