Custom DataGridViewTextBoxCell refreshing...

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I've created a custom class that inherits from the
DataGridViewTextBoxColumn and DataGridViewTextBoxCell. The cell is
databound to a text field that contains the path to a JPG image. The
JPG image is displayed in the cell if the picture is present on the
harddrive using Protected Overloads Overrides Sub Paint.

The problem is that the image is only displayed in the cell when the
cell receives the focus.

How can I get it to paint the picture in each visible row without the
row receiving focus first?
 
Here is a custom cell class that worked ok for me to show a 10x10
bitmap in a particular column in each row. Do the bitmaps you use fit
into the cell?

Public Class MyTextBoxCell
Inherits DataGridViewTextBoxCell

Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal
clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex
As Integer, ByVal cellState As DataGridViewElementStates, ByVal value
As Object, ByVal formattedValue As Object, ByVal errorText As String,
ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As
DataGridViewAdvancedBorderStyle, ByVal paintParts As
DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts)
Dim pt As Point = cellBounds.Location
pt.Offset(2, 2)
graphics.DrawImage(Bitmap.FromFile("Bitmap1.bmp"), pt)
End Sub 'Paint
End Class 'MyTextBoxCell

================
Clay Burch
Syncfusion, Inc.
 
I was using a Property called Image that would retrieve the image from
the file and for some reason the rowvalue wasn't being passed to it. I
got rid of it altogether and retrieve the image in the Paint sub.

I put the image into a picturebox the size I need the image to be and
then I pass the picturebox image. Thanks for your reply. You made me
rethink my code a little bit. Here's what I'm using:


Protected Overloads Overrides Sub Paint(ByVal graphics As Graphics,
ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal
rowIndex As Integer, ByVal cellState As DataGridViewElementStates,
ByVal value As Object, ByVal formattedValue As Object, ByVal errorText
As String, ByVal cellStyle As DataGridViewCellStyle, ByVal
advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal
paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts)

MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex,
cellState, value, formattedValue, _
errorText, cellStyle, advancedBorderStyle, paintParts)
Dim pt As Point = cellBounds.Location

Try
Dim OriginalImage As Image =
Image.FromFile(My.Settings.ImagePath + "\" + value)
Dim SmallerImage As New Bitmap(OriginalImage,
PictureBox1.Width, PictureBox1.Height)
PictureBox1.Image = SmallerImage
graphics.DrawImage(PictureBox1.Image, cellBounds.Location)
Catch
'do nothing
End Try
End Sub
 
Back
Top