Online images in DataGridView column

sgietz

Member
Joined
Jan 10, 2008
Messages
10
Programming Experience
3-5
My first post. Hello everyone :)

I have a DataGridView, and a DataTable. The first column in the DataTable is the name of an image (just a simple string). The second column is a description (also just a string). What I want to do is display the images in the first column.

I have code that works for a PictureBox (below):

VB.NET:
Dim ib As New PictureBox()
Dim MyWebClient As New System.Net.WebClient
Dim ImageInBytes() As Byte = MyWebClient.DownloadData("http://www.someurl/images/" & dr.Item(0))
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
ib.Image = New System.Drawing.Bitmap(ImageStream)
ib.SizeMode = PictureBoxSizeMode.StretchImage
ib.Size = New System.Drawing.Size(200, 100)

How can I make this work for the grid? I tried different variations with the image column to no avail, and it's beginning to drive me bonkers :(

Help is appreciated. Thanks :)
 
Last edited by a moderator:
VB.NET:
Dim client As New Net.WebClient
Dim url As String = "http://www.vbdotnetforums.com/images/smilies/smile.gif"
Me.DataGridView1.Rows(0).Cells(2).Value = New Bitmap(New IO.MemoryStream(client.DownloadData(url)))
 
Never mind .. I fixed it.

Thanks for pointing me in the right direction :)

For reference, here's what I did:

VB.NET:
Dim dc As New DataGridViewImageColumn
Dim client As New Net.WebClient
Dim url As String = String.Empty
dgv.Columns.Add(dc)

For i As Integer = 0 To dt.Rows.Count - 1
    url = "http://www.someurl.com/images/" & dt.Rows(i).Item(0)
    dgv.Rows(i).Cells(0).Value = New Bitmap(New IO.MemoryStream(client.DownloadData(url)))
Next
 
Last edited by a moderator:
Grrrrrrrrr ... had to modify the code for more than one row, and also added dimensions:


VB.NET:
Dim dc As New DataGridViewImageColumn
Dim client As New Net.WebClient
Dim url As String = String.Empty
dc.ImageLayout = DataGridViewImageCellLayout.Stretch
dc.Width = 200
dgv.Columns.Add(dc)
dgv.RowCount = dt.Rows.Count

For i As Integer = 0 To dt.Rows.Count - 1
url = "http://www.someurl.com/images/" & dt.Rows(i).Item(0)
dgv.Rows(i).Height = 100
dgv.Rows(i).Cells(0).Value = New Bitmap(New IO.MemoryStream(client.DownloadData(url)))
Next
 
Last edited by a moderator:
Back
Top