Training
Module
Guided Project - Work with Variable Data in C# - Training
Complete a guided project to apply knowledge of variable data in C#.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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);
}
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
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.
.NET Desktop feedback feedback
.NET Desktop feedback is an open source project. Select a link to provide feedback:
Training
Module
Guided Project - Work with Variable Data in C# - Training
Complete a guided project to apply knowledge of variable data in C#.