How to: Display Images in Cells of the Windows Forms DataGridView Control
A picture or graphic is one of the values that you can display in a row of data. Frequently, these graphics take the form of an employee's photograph or a company logo.
Incorporating pictures is simple when you display data within the DataGridView control. The DataGridView control natively handles any image format supported by the Image class, as well as the OLE picture format used by some databases.
If the DataGridView control's data source has a column of images, they will be displayed automatically by the DataGridView control.
The following code example demonstrates how to extract an icon from an embedded resource and convert it to a bitmap for display in every cell of an image column. For another example that replaces textual cell values with corresponding images, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.
Example
Public Sub CreateGraphicsColumn()
Dim treeIcon As New Icon(Me.GetType(), "tree.ico")
Dim iconColumn As New DataGridViewImageColumn()
With iconColumn
.Image = treeIcon.ToBitmap()
.Name = "Tree"
.HeaderText = "Nice tree"
End With
dataGridView1.Columns.Insert(2, iconColumn)
End Sub
private void createGraphicsColumn()
{
Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);
}
Compiling the Code
This example requires:
A DataGridView control named
dataGridView1
.An embedded icon resource named
tree.ico
.References to the System, System.Windows.Forms, and System.Drawing assemblies.
See Also
Tasks
How to: Customize Data Formatting in the Windows Forms DataGridView Control
Reference
Other Resources
Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control