Link to home
Start Free TrialLog in
Avatar of andreas_rafn
andreas_rafnFlag for Denmark

asked on

Images does not show in DataGridViewImageColumn cells (VB.NET).

I'm using a data grid view as a input control for a certain type of user input.
I wan't to implement the option of a user deleting records created, and I have begun doing so using a DataGridViewImageColumn with its image set as a red cross image (Windows 7 style), and a the click event (which deletes the associated row). However the image does not show when compiled, instead the default x image (image not found) appears. The image is part of the VS project resources and is 16x16

What can be causing the image to not show? Furthermore is there a better way to implement what I'm trying to implement?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Image is per row so you have to include that image as value in a column for each row in your data source (datatable).
Avatar of andreas_rafn


Ok, it works, however I'm wondering which event that should fire the image associating code so that:

The icon is ALWAYS shown for all visible rows (everytime a row is added by the user, and also the "new/empty) below last actual row
The icon is only associated once for each row

I tried to do it using the RowPrePaint event, but apparently the icon is painted extremely many times nonstop (it should only be associated once)
This seems to work, but is it the best way to do it?:

Private Sub Participants_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles InternalParticipantsDataGridView.RowsAdded, ExternalParticipantsDataGridView.RowsAdded        
        For Each Row As DataGridViewRow In Me.InternalParticipantsDataGridView.Rows
            If Row.Index = e.RowIndex Then Row.Cells(2).Value = My.Resources.DeleteCross16
End Sub

Open in new window

Add it when populating the row. Is grid bound to datatable? If so, add a column of type Image and add this image to that column for each row then grid will automatically paint it.
The grid is populated by user input only, otherwise I would have done as you said. Considering that is my way the best way?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial