Datagridview - which image?

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I have a datagridview and one of the columns is of type image.
In this image cell on each row there is an image taken from the resources file which, depending on the situation, could be my.resources.image1, my.resources.image2, my.resources.image3 and so on.

If, sometime later in my code, i want to know which image is in a given cell, how do i find that out?

I know how to detect if there is an image there (there always will be) but can't figure anything that lets me know exactly which image, such as name or similar.
 
Cannot believe i've asked this question again when i actually answered it myself last time i asked. I have no note of that solution hence asking again.
Now noted! Apologies all.

(Solution: image has a tag property - use that)
 
Last edited:
You should note that using My.Resources creates a new object each time. That means that if you get a My.Resources.SomeImage property twice then it will create two Image objects; they will look identical but they will not be the same object. That means that you should generally avoid accessing the same property of My.Resources more than once in a particular scope.

With that in mind, I would suggest getting all your resource images into Image variables and then you can use the Is operator at any time to determine whether any particular usage of an Image is that same one, e.g.
Private image1 As Image = My.Resources.Image1

'...

If myDataGridView.CurrentRow.Cells(imageColumnIndex).Value Is image1 Then
    'Image1 is displayed in the current row.
End If
EDIT: I just took a look at your original thread and you were comparing the Value of the cell to the My.Resources property and that was your downfall. Even apart from the fact that you were using '=' rather than 'is', as I said, that produces a new object each time so it can never work. If you need to prove that to yourself, try this code:
If My.Resources.SomeImage Is My.Resources.SomeImage Then
    MessageBox.Show("Same Image object.")
Else
    MessageBox.Show("Different Image objects.")
End If
 
This is superb advice and obviously something i didn't know, but do now.
I understand my mistake and am modifying my code to implement this now.
Thanks for taking the time to point this out to me, the learning continues...
 
Back
Top