Solved

Displaying image in datagrid from URL stored in database

Posted on 2007-11-14
5
964 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now