How to Add a Picture in a Datagridview by Browsing

In this tutorial we will create a program that browse a picture from your desired directory an load it in a picturebox and displayed in a datagridview.

You need:

1 Datagridview
3 Labels
1 Textbox
1 Combobox
1 Picturebox
1 Button

refer to the image below:



Start coding..


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ColumnCount = 2
        DataGridView1.Columns(0).Name = "Name"
        DataGridView1.Columns(1).Name = "Position"
        Dim imgCol As DataGridViewImageColumn = New DataGridViewImageColumn()
        imgCol.HeaderText = "Photo"
        imgCol.Name = "photo"
        DataGridView1.Columns.Add(imgCol)
    End Sub

    Private Sub Populate(ByVal name As String, ByVal position As String, ByVal img As Image)
        Dim row As Object() = New Object() {name, position, img}
        DataGridView1.Rows.Add(row)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Populate(TextBox1.Text, ComboBox1.SelectedItem, PictureBox1.Image)
        TextBox1.Text = ""
    End Sub

    Private Sub loadimage()
        Dim ofp As New OpenFileDialog()
        ofp.Title = "Select Photo"
        ofp.Filter = "JPEG Files|*.jpeg|JPG Files|*.jpg"
        If ofp.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(ofp.FileName)
        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        loadimage()
    End Sub
End Class


Final Output


How to Add a Picture in a Datagridview by Browsing How to Add a Picture in a Datagridview by Browsing Reviewed by code-dev on 5:35 PM Rating: 5

No comments:

Powered by Blogger.