How to programmatically set a selected item in a gallery in Power Apps Canvas apps

Hello everyone and welcome to my blog. In today’s blog I will discuss on how you can programmatically select a record in Power Apps canvas apps.

Many a times we need to select an item in the Gallery based on certain business logic. Additionally you may want to execute formula you have written in the OnSelect of an item in the Gallery.

To illustrate the requirement, I have taken a very simple example for this blog.

In the above screenshot, I have a gallery showing the list of accounts from DataVerse.

My requirement is pretty simple. On click of ‘Select Account 1‘ button, ‘Sample account 1‘ shall be selected. On click of ‘Select Account 2‘ button, ‘Sample account 2‘ shall be selected and so on.

While this demonstration is pretty simple, the logic to accomplish the requirement will be the same even for more complex app setups.

The first thing to ensure is to set the Default property of the Gallery to a variable. In my example, I have set the Default property of the Gallery to a variable selectedGalleryItem.

You may get an error at this point of time and that is because the variable is not defined yet. Don’t worry. We will fix it soon.

OnSelect of ‘Select Account 1‘ button, I used the below expression.

Below is the expression of OnSelect of the ‘Select Account 2‘ button.

In the OnSelect of the Gallery, I have set the following expression. So whenever a new item is selected, it will Notify me with the selected account name.

Now let’s see this whole stuff in action.

In the video illustration, you can see that while I am able to change the selected item in the gallery, I am not able to trigger OnSelect of the Gallery.

The first requirement to programmatically change the selected item of a Gallery is working. The next step is to ensure that the OnSelect of the Gallery is triggered.

To accomplish the requirement, I need to change the button OnSelect formula to the expression in the below screenshot.

I have added one additional expression to Select the Gallery control.

And that will be all. Now when I select the buttons, the selected item will keep on changing and notification for the selected item will display accordingly.

Hope this helped.

You will also like the below posts.

Debajit Dutta


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

1 thought on “How to programmatically set a selected item in a gallery in Power Apps Canvas apps”

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading