web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Selecting gallery item...
Power Apps
Answered

Selecting gallery item based on context variable

(0) ShareShare
ReportReport
Posted on by 193

Hi all, 

 

What I am trying to do seems pretty simple but since there is no "SelectGalleryRow" behavior function, I am having trouble figuring it out or finding a solution that works for my situation. 

 

I have a gallery shown and a "New Item" button which allows creation of a new record for that same table. Upon saving, I am patch that record into the table and hold the ID as a context variable. Now I want the gallery to immediately select that record. I thought this would be done by setting the Default of the gallery to:

First(Filter(Table1, ID = varNewID))

This doesn't seem to work though, and in fact I have trouble getting the Default property to do what I want it to do at all. Even setting that property as

First(Filter(Table1, ID = 3))

And then changing the number around to other numbers does not seem to select the appropriate record when using the Reset behavior for the gallery. It even seems that if I used that function set to, say, ID = 2 it will work the first time, but if I change it to ID=3, the Reset will still highlight the ID = 2 record. 

Please help!

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @cah2035 

    The default property is highly dependent on the records in the Gallery.

    What is your Items property formula?

  • CAH2035 Profile Picture
    193 on at

    Hi Randy, 
    Items property is "Table1". Currently, I am not filtering the items so I am just showing everything in the standard order. 

    I realized something else that might be an option is using the Select function:
    Select(gallery1, 3) to select the third row of the gallery, however, I don't know how I would replace the "3" with the specific row number of the record I want, or if that is even possible. 

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @cah2035 

    Doing the select is more trouble than it is worth considering what you have already determined...how would you know the row?

     

    If you are not doing anything in particular in your Items property (and it appears not), then you should be able to set the Default property of the Gallery to Lookup(Table1, ID=varNewID)

    That will return the proper record to make the default. 

    The next question I would have, if that's not working...what is varNewID?  Does it have a value as expected?

  • Digital Profile Picture
    1,207 on at

    You could try putting the newly created record into a global variable and using that as the default.

  • CAH2035 Profile Picture
    193 on at

    @RandyHayes 

     

    I just tested this out, and here is what happens:

    gallery1.Default = LooUp(P1_Investigators, ID = cvSelectInvestigator)
    SaveButton.OnSelect = 

    Set(cvSelectInvestigator, // Sets variable to the ID of this newly patched record
     Patch( //Patching the new record into the table
     P1_Investigators,
     Defaults(P1_Investigators),
     {
     InvestigatorName: txt_1_InvestigatorName.Text,
     InvestigatorEmail: txt_1_InvestigatorEmail.Text,
     SecondaryContactName: txt_1_SecondaryContactName.Text,
     SecondaryContactEmail: txt_1_SecondaryContactEmail.Text,
     InstitutionName: txt_1_InstitutionName.Text,
     DepartmentName: txt_1_DepartmentName.Text
     }
    ).ID);
    UpdateContext({cvNewInvestigatorAdd: false}); //Go into "view" mode 
    Reset(gal_1_InvestigatorList) //Reset gallery to Default

     

    My variable cvSelectInvestigator is correctly set to the ID number of the new record. Upon reset of the gallery, it should Default to 

    LookUp(P1_Investigators, ID = 16) but it does not. Even when I have a button with the same Reset function, it is defaulting to the first item at the top of the gallery. Even if I change the function to remove the variable and just put "16" in its place, it still defaults to the first item. 

    I have also tried switching LookUp with First(Filter but have the same result. 

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @cah2035 

    I never asked or noticed...are we dealing with SharePoint??  Your first examples mentioned Table1, so I had a sense of Excel. Now with your last post, your datasource name and column names feel more like SharePoint.

     

    If so, change your Gallery Items property to this formula:

        DropColumns(P1_Investigators, "{Attachments}")

    Then change your Default on the Gallery to:

        Lookup(DropColumns(P1_Investigators, "{Attachments}"), ID= cvSelectInvestigator)

     

    If not...the investigation goes on.

     

  • CAH2035 Profile Picture
    193 on at

    That completely worked! Thank you! What exactly is it about Attachments in a Sharepoint list that caused this issue?

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @cah2035 

    Yay!!!

    That's a good question.  I've never taken the time to digest the issue, I just know that I run into it often with SharePoint lists.  

    Keep in mind also, if you Collect a SharePoint list, the Attachments field will not be in the collection.  Oddly enough, a Lookup will include attachments and a Gallery based on a SharePoint source will include them also.  But, somehow the comparison for the Default property does not compare unless you drop them.

    Something to keep in your back pocket for another experience. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 429 Most Valuable Professional

#2
11manish Profile Picture

11manish 191 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 122 Super User 2026 Season 2

Last 30 days Overall leaderboard