How to show image from URL in datagridview cell?

前端 未结 2 1482
清歌不尽
清歌不尽 2021-01-27 03:28

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

2条回答
  •  滥情空心
    2021-01-27 03:53

    Here is my solution. It works correctly for me to retrieve an image from DataGridView to load in PictureBox.

    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

提交回复
热议问题