How to show image from URL in datagridview cell?
Have a look at this SO Post https://stackoverflow.com/a/1906625/763026
foreach (DataRow row in t.Rows)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["uri"].ToString());
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
myResponse.Close();
row["Img"] = bmp;
}
Related videos on Youtube
M W
Updated on September 15, 2022Comments
-
M W over 1 year
How do you load an image from a URL and then put it into DataGridView's cell (not Column header)? The rows which include the images will be added to the view at runtime based on a search from web service. Cannot find an answer for this specific purpose... Help!
First I tried using PictureBox. When events are received from web service, I will loop thru result to add rows, each of which includes an image.
// Add image System.Windows.Forms.PictureBox picEventImage = new System.Windows.Forms.PictureBox(); picEventImage.Image = global::MyEventfulQuery.Properties.Resources.defaultImage; picEventImage.ImageLocation = Event.ImageUrl; this.dgvEventsView.Controls.Add(picEventImage); picEventImage.Location = this.dgvEventsView.GetCellDisplayRectangle(1, i, true).Location;
Even though the image loads perfectly, it looks disconnected from the view, i.e. images does not move when I scroll, and when refreshing the view with new data, images just hang around... bad.
So I tried tips from other postings:
Image image = Image.FromFile(Event.ImageUrl); DataGridViewImageCell imageCell = new DataGridViewImageCell(); imageCell.Value = image; this.dgvEventsView[1, i] = imageCell;
But I got an error saying "URI formats are not supported."
- Am I using Image incorrectly?
- Is there another class that I can use for URL image instead of Image?
- Or do I have no choice but to create a custom control (which contains a PictureBox) to add to the DataGridView cell?
-
M W almost 12 yearsThanks I think this would lead me to an answer. So to answer my own questions, I should use the DataGridViewImageCell/Column, and load the image dynamically following the instruction. I also looked at the option of creating a custom control inherited from the abstract DataGridViewCell, and got stuck at having to do the manual painting. See this[devolutions.net/articles/Dot-net/DataGridViewFAQ/… which explains why I can't (easily) do that.