如何在datagridview的image列中放入noone图像

时间:2012-12-16 00:47:33

标签: vb.net

我必须将图像放到datagridview的特定列,但不是每行。

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

使用此代码我执行时可能会出错,因为我试图在image列中放置一个字符串。我尝试的第二个是:

.Cells("myCol").Value = Nothing

这不会导致错误,但会将“错误图像”图片(带红色X)放到网格中。

这是一种在没有从文件或资源中加载“空白图像”的情况下将任何一个图像(空白)放入datagridview的图像列的方法吗?

3 个答案:

答案 0 :(得分:3)

过去我必须这样做,只使用1像素透明PNG。在我的应用程序中,性能是可以接受的,即使有18个图像列和~2000行,并且单元格的背景颜色也很好。

您应该能够使用免费程序(如“Paint.Net”)轻松创建1x1像素透明PNG。

答案 1 :(得分:0)

您可以创建一个临时空白位图并将其分配给而不是从文件加载图像。

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

答案 2 :(得分:0)

可以提高性能的稍微好一点的方法:

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