Results 1 to 5 of 5

Thread: Datagridview - which image?

  1. #1
    DavyEFC is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    44
    Reputation
    80

    Datagridview - which image?

    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.

  2. #2
    DavyEFC is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    44
    Reputation
    80
    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 by DavyEFC; 11-09-2016 at 6:49 AM.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,814
    Reputation
    1707
    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

  5. #5
    DavyEFC is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    44
    Reputation
    80
    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...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •