VB NET - How To Add Image To DataGridView From PictureBox Using VB.NET

insert image to datagridview using picturebox

VB.NET - How To Insert Images Into DataGridView From PictureBox In VBNET

                                                                                                                         

In This VB.NET Tutorial We Will See How To Browse And Display Image Into PictureBox In VB.NET Programming Language.


Project Source Code:

Imports System.IO

Public Class Add_Image_To_DataGridView_From_PictureBox

   
    Private Sub Add_Image_To_DataGridView_From_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"
        dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch

        Dim dgvTextColumn As New DataGridViewTextBoxColumn
        dgvTextColumn.HeaderText = "Id"

        DataGridView1.Columns.Add(dgvTextColumn)
        DataGridView1.Columns.Add(dgvImageColumn)

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        DataGridView1.RowTemplate.Height = 120
        DataGridView1.AllowUserToAddRows = False

    End Sub
    ' button load image into picturebox
    Private Sub BtnBrowseImage_Click(sender As Object, e As EventArgs) Handles BtnBrowseImage.Click
        Dim opf As New OpenFileDialog
        opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"

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

    ' button add image
    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click

        Try
            Dim ms As New MemoryStream
            PictureBox1.Image.Save(MS, PictureBox1.Image.RawFormat)
            Dim img As Byte()
            img = ms.ToArray()
            DataGridView1.Rows.Add(TextBox1.Text, img)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

    End Sub
End Class

///////////////OUTPUT:





Vb net insert image to datagridview using picturebox
Vb net insert image to datagridview using picturebox

Share this

Related Posts

Previous
Next Post »

1 comments:

comments
13 octobre 2016 à 07:05 delete

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

When i want to add image from picturebox to datagridview ????

Reply
avatar