繁体   English   中英

如何将没有人的图像放在datagridview的图像列中

[英]How to put noone image in image column of datagridview

我必须将图像放置到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的图像列的方法吗?

我过去不得不这样做,只是使用了1像素的透明PNG。 在我的应用程序中,即使有18个图像列和约2000行,该性能也是可以接受的,并且该单元格的背景色可以很好地显示出来。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM