Adding Image to DataGridView in VB.NET

The DataGridView control and its related classes offer great flexibility and extensibility for displaying and editing tabular data. Among the available column types, we can utilize an Image column to add an Image control within a DataGridView.

Image and ImageLayout

The Image column type provides additional properties, such as Image and ImageLayout, in addition to the standard properties inherited from the base class. By setting the Image property of the column, we can ensure that the specified image is displayed by default for all the cells in that column.

Manually populating an image column becomes beneficial when you want to offer functionality similar to that of a DataGridViewButtonColumn, but with a customized appearance. This allows you to provide a visual representation or indicator within the DataGridView that can be associated with specific data or actions.

The following vb.net program shows how to add a Image in column of a DataGridView control.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGridView1.ColumnCount = 3 DataGridView1.Columns(0).Name = "Product ID" DataGridView1.Columns(1).Name = "Product Name" DataGridView1.Columns(2).Name = "Product_Price" Dim row As String() = New String() {"1", "Product 1", "1000"} DataGridView1.Rows.Add(row) row = New String() {"2", "Product 2", "2000"} DataGridView1.Rows.Add(row) row = New String() {"3", "Product 3", "3000"} DataGridView1.Rows.Add(row) row = New String() {"4", "Product 4", "4000"} DataGridView1.Rows.Add(row) Dim img As New DataGridViewImageColumn() Dim inImg As Image = Image.FromFile("Image Path") img.Image = inImg DataGridView1.Columns.Add(img) img.HeaderText = "Image" img.Name = "img" End Sub End Class