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
Form Event:
Private con As New SqlConnection("YourConnectionString")
Private com As SqlCommand
Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
con.Open()
com = New SqlCommand("SELECT MyPhoto FROM tbGalary WHERE ID=" & DGV.Rows(e.RowIndex).Cells(0).Value, con)
Dim ms As New MemoryStream(CType(com.ExecuteScalar, Byte()))
txtPicture.Image = Image.FromStream(ms)
txtPicture.SizeMode = PictureBoxSizeMode.StretchImage
com.Dispose()
con.Close()
End Sub
SQL Table:
CREATE TABLE [dbo].[tbGalary](
[ID] [int] NOT NULL,
[MyPhoto] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
SQL Insert image:
INSERT INTO tbGalary VALUES('1','D:\image1.jpg')
INSERT INTO tbGalary VALUES('2','D:\image2.jpg')
INSERT INTO tbGalary VALUES('3','D:\image3.jpg')
INSERT INTO tbGalary VALUES('4','D:\image4.jpg')
INSERT INTO tbGalary VALUES('5','D:\image5.jpg')
Result
Video link: Retrieve an image in DataGridView load to PictureBox in VB.NET
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;
}