Shoshotto
Shoshotto

Reputation: 93

DataGridView Button Column not showing image on CellPainting

I have a DataGridView with one button column. When I use the following code with image unblock3.png the image is displayed, but when I replace it with another image check1.png nothing is displayed.

I tried resizing check1.png to smaller size but it still not working.

Could you please help me to figure out what is wrong?

Private Sub ShiftsList_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles ShiftsList.CellPainting
    If ShiftsList.Columns(e.ColumnIndex).Name = "ConfirmShift" AndAlso e.RowIndex >= 0 Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All)
        e.Graphics.DrawImage(My.Resources.unblock3, CInt((e.CellBounds.Width / 2) - (My.Resources.unblock3.Width / 2)) + e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.unblock3.Height / 2)) + e.CellBounds.Y)
        e.Handled = True
    End If
End Sub

ResourcesFolder

UnChecked

Checked

Upvotes: 0

Views: 666

Answers (1)

Shoshotto
Shoshotto

Reputation: 93

I think the image was displayed, but too small for a human eyes to recognize.

The only thing I needed to add is to provide the draw command with the required width and height.

If FilesList.Columns(e.ColumnIndex).Name = "Unblock" AndAlso e.RowIndex >= 0 Then
        e.Paint(e.CellBounds, DataGridViewPaintParts.All)
        e.Graphics.DrawImage(My.Resources.unblock3, CInt((e.CellBounds.Width / 2) - (My.Resources.unblock3.Width / 2)) + e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.unblock3.Height / 2)) + e.CellBounds.Y, My.Resources.unblock3.Width, My.Resources.unblock3.Height)
        e.Handled = True
    End If

My images are small 32x32 so I provided their original dimensions.

enter image description here

Upvotes: 1

Related Questions