如何将DataGrid图像列上的X图像设为空白图像? - VB.Net

How to make X Image on DataGridImageColumn as Blank Image? - VB.Net

大家安心,我遇到了 datagridimagecolumn 的问题,如果特定行上没有上传字节图像,它会显示 x 图像。这是下图,向您展示我具体指的是什么。

所以,我在图片框上显示图像没有问题,但是,我的问题是 [red x] 图像。如何将其设为空白图像或空图像?

这是我的代码,用于将我的数据从数据库显示到 datagridview,并使用自动拉伸图像列的代码(也许它可以帮助我?)。

'------------REFRESH DISPLAY------------'
Public Sub RefreshUser()
    '------------CONNECTION DATABASE------------'
    Dim connectionUser As New SqlConnection(_1LoginForm.connectionstring)
    '------------QUERY------------'
    sqlUser = "SELECT * FROM [1UserAccounts]"
    '------------START CONNECTION------------'
    connectionUser.Open()
    '------------SQL------------'
    sCommandUser = New SqlCommand(sqlUser, connectionUser)
    sAdapterUser = New SqlDataAdapter(sCommandUser)
    sBuilderUser = New SqlCommandBuilder(sAdapterUser)
    sDsUser = New DataSet()

    sAdapterUser.Fill(sDsUser, "User")
    sTableUser = sDsUser.Tables("User")

    '------------SET OF BINDING SOURCE------------'
    Userbindingsource.DataSource = sDsUser.Tables("User")

    '------------CLOSE CONNECTION------------'
    connectionUser.Close()

    '------------DATA BINDINGS------------'
    DGVViewEditForm.DataSource = Userbindingsource
    BNViewEditForm.BindingSource = Userbindingsource


    TBUserAccountID.DataBindings.Clear()
    TBUserCompanyID.DataBindings.Clear()
    TBUserFullName.DataBindings.Clear()
    TBUserName.DataBindings.Clear()
    TBUserPassword.DataBindings.Clear()
    TBUserRole.DataBindings.Clear()

    '------------DATA BINDINGS ADDITION------------'
    TBUserAccountID.DataBindings.Add("text", Userbindingsource, "UserAccountID")
    TBUserCompanyID.DataBindings.Add("text", Userbindingsource, "UserCompanyID")
    TBUserFullName.DataBindings.Add("text", Userbindingsource, "UserFullName")
    TBUserName.DataBindings.Add("text", Userbindingsource, "UserName")
    TBUserPassword.DataBindings.Add("text", Userbindingsource, "UserPassword")
    TBUserRole.DataBindings.Add("text", Userbindingsource, "UserRole")

    '------------DATA GRID VIEW SELECTION------------'
    DGVViewEditForm.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DGVViewEditForm.MultiSelect = False


    '------------STRETCHES COLUMNS WITH IMAGES------------'
    For i As Integer = 0 To DGVViewEditForm.ColumnCount - 1
        If TypeOf DGVViewEditForm.Columns(i) Is DataGridViewImageColumn Then
            DirectCast(DGVViewEditForm.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch

        End If
    Next

End Sub

我的问题就是这些,我希望有人能帮助我解决这个问题。提前致谢!

您可以处理 CellFormatting 事件来调解并提供不同的图像。对于完全空白的外观,您将需要一个只是透明背景的小 PNG 图片。

以下代码将提供一个问号图像作为缺失图像的占位符,并为新用户行提供一个加号图标:

' form level declarations:
Private NewRowImage As Image
Private NoImage As Image
...
' Assign them in the ctor or form load:
NewRowImage = My.Resources.add
NoImage = My.Resources.question

不要 use/reuse 直接来自 My.Resources 的图像,以防止为每一行创建新的图像对象。 CellFormatting 事件:

' use your column index:
If e.ColumnIndex = 5 Then
    If dgv1.Rows(e.RowIndex).IsNewRow Then
        e.Value = NewRowImage
        e.FormattingApplied = True
    ElseIf (e.Value Is Nothing) OrElse (DBNull.Value.Equals(e.Value)) Then
        e.Value = NoImage
        e.FormattingApplied = True
    End If
End If

您可能只需要测试 DBNull,Nothing 测试只是为了安全起见。结果: