Reputation: 4655
I have to put an image to specific column of datagridview but not in every row.
If getExists(myTable, CInt(reader.GetValue(0)), dbConn) Then
.Cells("myCol").Value = Image.FromFile("C:\myPath\myIco_16x16.ico")
Else
.Cells("myCol").Value = "" ' error here
End If
With this code I get error while executing probably because I'm try to put a string in image column. Second I try is:
.Cells("myCol").Value = Nothing
This don't cause error but put "error image" picture (with red X) to grid.
Is here a way to put no one image (blank) to datagridview's image column without loading a "blank image" from file or resource?
Upvotes: 2
Views: 4428
Reputation: 49
Slightly better method that can improve performance:
Friend Function BlankImage() As Image
Static oBM As New Bitmap(1, 1)
Try
If oBM Is Nothing Then
oBM.SetPixel(0, 0, Color.Transparent)
End If
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function
Upvotes: 0
Reputation: 11
You could just create a temporary blank bitmap and assign this to rather than loading an image from file.
Friend Function BlankImage() As Image
Try
Dim oBM As New Bitmap(1, 1)
oBM.SetPixel(0, 0, Color.Transparent)
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function
Upvotes: 1
Reputation: 4647
I've had to do this in the past and just used a 1 pixel transparent PNG. The performance was acceptable in my application even with 18 image columns and ~2000 rows and the background color of the cell shows through it just fine.
You should be able to create a 1x1 pixel transparent PNG fairly easily using a free program such as "Paint.Net".
Upvotes: 3