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 / Select an item in the ...
Power Apps
Answered

Select an item in the gallery programmatically

(4) ShareShare
ReportReport
Posted on by 234

We have a canvas app that displays a gallery representing site visits where each site visit has its own unique ID. What we would like to do is when one creates a new site visit in the AddEditScreen is:

  1. Update the SiteVisitsGallery with the new or updated site visits (done)
  2. Navigate the user to the OverviewScreen with contains the updated SiteVisitsGallery (done)
  3. Open the DetailsScreen by selecting the correct gallery item based on the site visit unique ID

Step 3 is a bit of pain because we can't figure out the correct row number in the gallery to use this code, which essentially does what we want it to do if we hardcode a row number:

 

Select(SiteVisitsGallery, UnknownRowNumber, SelectiItemRectangle);

 

The row number could be added to the collection but "ForAll" is not sequential so that's not an option.

 

The other approach we tried is selecting the correct site visit in the SiteVisitsGallery which works fine and we can see the correct row being selected:

 

// SiteVisitsGallery
Default = varSelectedSiteVisitRow
OnSelect = Select(SelectiItemRectangle)

// button.OnSelect:
Reset(SiteVisitsGallery);
Set(varSelectedSiteVisitRow, LookUp(colSiteVisits, SiteVisit.ID = 11));

 

We got this far but for the life of me I can't figure out how to do the actual click on the selected row in the gallery. The color indicates the row is correctly selected but we don't know how to invoke the "SiteVisitsGallery.OnSelect" for the selected item programmatically.

 

Any tips or help would be welcomed, thank you.

Categories:
  • Verified answer
    DarkLiteOne Profile Picture
    234 on at

    OMG, I figured this one out by accident... What you have to do is:

    1. Reset the gallery (not strictly needed)
    2. Set the "Default" variable of the gallery
    3. Then use "Select" on the gallery so it will trigger the "OnSelect" on the correct row

     

    // button
    Reset(SiteVisitsGallery);
    Set(varSelectedSiteVisitRow, LookUp(colSiteVisits, SiteVisit.ID = 11));
    Select(SiteVisitsGallery);

     

    Hopefully this will help others too.

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Did you tried to set Default property on Gallery like 

    LookUp(DataSource, ID = myVariableID) 

     

     

  • chienhsianghung Profile Picture
    235 on at

    2. Set the "Default" variable of the gallery

    Set(varSelectedSiteVisitRow, LookUp(colSiteVisits, SiteVisit.ID = 11));

     

    saved my day! thank you

  • LL_Yoor Profile Picture
    9 on at

    Hello, I am trying to follow your solution here but am a bit stuck with how to code the OnSelect property of the gallery. In your code you have:

    OnSelect = Select(SelectiItemRectangle)

    Can you explain what is going on there?

    Thanks!

  • cha_cha Profile Picture
    4,932 Moderator on at

    Thank you for posting your solution.

  • Azalia20 Profile Picture
    34 on at

    Hi, its nice to know you!
    I don't understand this, could you please explain me in detail or is there a tutorial I could see for this?

  • Golly Profile Picture
    127 on at

    Thanks @DarkLiteOne! Your solution worked great.  I used local variables (UpdateContext) since all my items are on the same screen instead of global variable (Set) which goes between screens, but either will work, depending on your situation.  My use case is that I have an edit form and gallery on the same page.  When the edit form is saved, it resets the gallery, but I want the gallery to remain on the same item.

     

    The steps I used are:

     

    Set the OnSelect property of a button (or a control with an OnSelect property) to:

     

     

    UpdateContext({varSelectItem:LookUp('SourceName', ThisRecord.ID=varPickThisOne)});Select(GalleryName)

     

     

     

    When the button is pressed, this will set varSelectItem to the record with the ID of varPickThisOne and select the item in your gallery.  I used ThisRecord.ID because I want the item that is already picked in the gallery to be picked again after the button does its other tasks (saving the form data to the list) - you'll want to point this at whatever your data source is for the ID.

     

    Set the gallery OnSelect property to:

     

     

    UpdateContext({varPickThisOne:GalleryName.Selected.ID})

     

     

         This will set varPickThisOne to the ID of the currently selected item in the gallery.

     

    Set the gallery Default property to: 

     

     

     

    varSelectItem

     

     

     

    This sets the default item (the one that displays) in the gallery to the record with the ID of varPickThisOne, which we set in the previous step.

  • oladimejishodip Profile Picture
    16 on at

     

    updateresult.png

    @Golly ,@DarkLiteOne. 

    Thank you for the solution. However, I got this error (Notice) trying to implement the code. All the items as well the gallery are on the same screen. I will appreciate your help.

     

    These are my codes:

    Gallery: Onselect

    UpdateContext({PreviouslySelectedRow:Gallery1_1.Selected.'Summary Table Key'});

    Gallery: Default:

    PreviouslySelectedRowV2

     

    Button: OnSelect:

    UpdateContext({PreviouslySelectedRowV2: LookUp('SIPGridData10', ThisRecord.'Summary Table Key'=PreviouslySelectedRow)});Select(Gallery1_1);

     

     

  • Golly Profile Picture
    127 on at

    A couple questions:

    1. Is 'SIPGridData10' the data source for Gallery1_1?

    2. Is 'Summary Table Key' a value or a control?  From the error, it looks like you may have a control selected, not the value within it.  

  • oladimejishodip Profile Picture
    16 on at

    1.  Yes, 'SIPGridData10'  is the the data source for Gallery1_1.

    2. 'Summary Table Key' is the ID column in the 'SIPGridData10'

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
11manish Profile Picture

11manish 402 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 296 Most Valuable Professional

Last 30 days Overall leaderboard