How to insert images into a GridView column for a Windows Forms application?

I have a GridView in a windows forms control application. I am loading the Gridview from a DataTable object.

DataGridView.DataSource = table

The table has two columns. The first column will either contain the number 1, or nothing at all. The second column contains the names of members on a soccer team.

What I want to do is to use the "1" in the first column as a type of flag. If their is a "1" in that column I need to insert an image. So the image will replace the number 1. Note the same image will be used for every row which has the "1" in it.

I need the image to be inserted into the GridView row before the GridView is displayed on the users screen. Does anyone know how I can do this?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Here is one way you could accomplish this:

Form1.vb -
Public Class Form1
	Private Team As New List(Of Member) From {New Member() With {.Gender = 1, .Name = "Billy"}, New Member() With {.Gender = 2, .Name = "Susan"}, New Member() With {.Gender = 2, .Name = "Mary"}, New Member() With {.Gender = 1, .Name = "John"}}

	Private Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
		Binder.DataSource = Team
		SoccerTeam.DataSource = Binder
	End Sub


	Private Sub OnCellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles SoccerTeam.CellFormatting
		If TypeOf sender Is DataGridView Then
			Dim grid As DataGridView = DirectCast(sender, DataGridView)
			If TypeOf grid.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewImageCell Then
				If grid.Columns(e.ColumnIndex).DataPropertyName.Equals("Gender") Then
					e.Value = My.Resources.male
					e.FormattingApplied = True
				End If
			End If
		End If
	End Sub

	Private Sub OnRowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventArgs) Handles SoccerTeam.RowsAdded
		If TypeOf sender Is DataGridView Then
			Dim grid As DataGridView = DirectCast(sender, DataGridView)
			For Each row As DataGridViewRow In grid.Rows
				If TypeOf row.DataBoundItem Is Member Then
					Dim [member] As Member = DirectCast(row.DataBoundItem, Member)
					If [member].Gender.Equals(1) Then
						row.Cells("Gender") = New DataGridViewImageCell()
					Else
						row.Cells("Gender") = New DataGridViewTextBoxCell()
					End If
				End If
			Next
		End If
	End Sub
End Class

Class Member
	Public Property Gender() As Integer
	Public Property Name() As String
End Class

Open in new window


Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.components = New System.ComponentModel.Container()
		Me.SoccerTeam = New System.Windows.Forms.DataGridView()
		Me.Binder = New System.Windows.Forms.BindingSource(Me.components)
		CType(Me.SoccerTeam, System.ComponentModel.ISupportInitialize).BeginInit()
		CType(Me.Binder, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'SoccerTeam
		'
		Me.SoccerTeam.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
		Me.SoccerTeam.Dock = System.Windows.Forms.DockStyle.Fill
		Me.SoccerTeam.Location = New System.Drawing.Point(0, 0)
		Me.SoccerTeam.Name = "SoccerTeam"
		Me.SoccerTeam.Size = New System.Drawing.Size(284, 262)
		Me.SoccerTeam.TabIndex = 0
		'
		'Binder
		'
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 262)
		Me.Controls.Add(Me.SoccerTeam)
		Me.Name = "Form1"
		Me.Text = "Form1"
		CType(Me.SoccerTeam, System.ComponentModel.ISupportInitialize).EndInit()
		CType(Me.Binder, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents SoccerTeam As System.Windows.Forms.DataGridView
	Friend WithEvents Binder As System.Windows.Forms.BindingSource

End Class

Open in new window

Produces the following results -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.