VB.NET - How To Update DataGridView Image With PictureBox Using VB.NET


vb update datagridview row image

VB.NET - How To Update DataGridView Row Image Using PictureBox In VBNET

                                                                                                                         

In This VB.NET Tutorial We Will See How To Update DataGridView Cell Images Using PictureBox In VBNET Programming Language.


Project Source Code:

Imports System.IO
Imports System.Drawing.Imaging

Public Class VB_Update_DataGridView_Row_Image_Using_PictureBox

    Private Sub VB_DataGridView_Row_Image_Using_PictureBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '  Create Datagridview image column
        Dim dgvImageColumn As New DataGridViewImageColumn

        ' set header text to the column
        dgvImageColumn.HeaderText = "Image"

        ' make the image layout stretch to display the entire image
        dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch

        ' add the column to the datagridview
        DataGridView1.Columns.Add(dgvImageColumn)

        ' make the columns take all the datagridview width
        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        ' change the datagridview height
        DataGridView1.RowTemplate.Height = 150

        DataGridView1.AllowUserToAddRows = False


        ' create images and add the to the datagridview
        Dim img1 As Image
        img1 = Image.FromFile("D:\Images\img10.jpg")
        DataGridView1.Rows.Add(img1)

        Dim img2 As Image
        img2 = Image.FromFile("D:\Images\img20.jpg")
        DataGridView1.Rows.Add(img2)

        Dim img3 As Image
        img3 = Image.FromFile("D:\Images\img30.jpg")
        DataGridView1.Rows.Add(img3)

        Dim img4 As Image
        img4 = Image.FromFile("D:\Images\img40.jpg")
        DataGridView1.Rows.Add(img4)

        Dim img5 As Image
        img5 = Image.FromFile("D:\Images\img50.jpg")
        DataGridView1.Rows.Add(img5)
    End Sub

    ' DataGridView cell click
    ' Get Image From DataGridView To PictureBox
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        Dim ms As New MemoryStream

        Dim img As Bitmap

        img = DataGridView1.CurrentRow.Cells(0).Value

        img.Save(ms, ImageFormat.Jpeg)

        PictureBox1.Image = Image.FromStream(ms)

    End Sub

    ' Browse Image Into PictureBox
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

        Dim opf As New OpenFileDialog
        opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"

        If opf.ShowDialog = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(opf.FileName)
        End If

    End Sub

    ' button update image
    Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click

        Dim ms As New MemoryStream

        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)

        Dim img As Byte()

        img = ms.ToArray()

        DataGridView1.CurrentRow.Cells(0).Value = img

    End Sub
End Class
///////////////OUTPUT:





vb update datagridview row image using picturebox
vb update datagridview cell image using picturebox




Share this

Related Posts

Previous
Next Post »