Raniel Quirante
Raniel Quirante

Reputation: 315

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

Peace everyone, I am having this issues with the datagridimagecolumn, where it shows the x image if there are no byte image uploaded on the specific row. this is the image below to show you what I am referring to specifically.

So, I have no problem showing the image on the picture box, but, my problem is that [red x] image. how can I make it as a blank image or empty image?

This is my code to show my data from database to datagridview with a code that auto stretches the image column (maybe it could help to help me?).

'------------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

That's it for my question, I hope someone could help me with this. Thanks in advance!

Upvotes: 1

Views: 724

Answers (1)

You can handle the CellFormatting event to intercede and supply a different image. For a totally blank appearance, you will need a small PNG image which is just a transparent background.

The following code will provide a question mark image as a placeholder for a missing image, and a plus sign icon for the new user row:

' 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

Dont use/reuse the image from My.Resources directly to prevent a new image object being created for each row. The CellFormatting event:

' 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

You probably only need to test for DBNull, the Nothing test is just to be safe. The Result:

enter image description here

Upvotes: 1

Related Questions