Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Controls and Formulas

The Gallery Control

Master the Power Apps gallery: Items, ThisItem, Selected, templates, layouts, wrap count, nested galleries, buttons inside rows, and ForAll, Sum and CountRows.

The gallery is the control you will use most. It repeats a template once for each record in a table and is the usual way to show lists, cards and menus. This lesson explains how it works and how to build the common patterns.

Items and the template

A gallery has an Items property: any formula returning a table. For each record, the gallery draws its template, which is the first row you edit. Controls inside the template can read the current record with ThisItem.

TEXT
Items: Tasks
// Label inside the template:
Text: ThisItem.Title

Select the pencil icon of the gallery to edit the template. When you add a control inside the template, it appears in every row.

Layouts

Gallery layouts include vertical, horizontal, flexible height, and blank variants. Blank galleries give you an empty template to build from scratch. Use TemplatePadding and TemplateSize to control spacing and row height. Wrap count makes a vertical gallery flow into several columns, ideal for card grids.

Selected and Default

When a user taps a row, it becomes selected. Read the selected record with galTasks.Selected, for example galTasks.Selected.Title. Control which row starts selected with the Default property:

TEXT
Default: First(Tasks)      // or a lookup of a known record

Set AllowSelection to false if rows are display only. The TemplateFill property can highlight the selected row:

TEXT
TemplateFill: If(ThisItem.IsSelected, RGBA(220, 235, 250, 1), Transparent)

Buttons and actions inside rows

Controls in the template can act on ThisItem:

TEXT
// An icon in each row
OnSelect: Remove(Tasks, ThisItem)
// A checkbox
Default: ThisItem.Done
OnCheck: Patch(Tasks, ThisItem, {Done: true})

A button inside the template does not change the gallery's selected item unless you also set it, so use ThisItem or set a variable first: Set(varTask, ThisItem).

Counting, summing and looping

Aggregate functions work on any table:

TEXT
CountRows(Filter(Tasks, Done = false))
Sum(galOrders.AllItems, Qty * Price)

gallery.AllItems returns the full table the gallery is showing. For actions on many records, use ForAll:

TEXT
ForAll(galOrders.AllItems, Patch(Orders, ThisRecord, {Status: "Packed"}))

ForAll runs a formula for each record (inside it, the current record is ThisRecord). It is a behavior function, so it goes in OnSelect-style properties.

The stand-in below models a gallery: it repeats a template (a function) for each record, tracks a selected row and computes Sum and CountRows.

JS Stand-in: a gallery repeats a template per record

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Use functions in Items:

TEXT
SortByColumns(Filter(Tasks, Done = false), "Due", SortOrder.Ascending)

Controls above the gallery (search box, dropdown filter, sort toggle) feed the formula:

TEXT
Filter(Tasks,
  (IsBlank(drpOwner.Selected.Value) || Owner = drpOwner.Selected.Value),
  (txtSearch.Text in Title))

Watch delegation: with big data sources some functions are evaluated only on the first rows (see Delegation and data sources).

The stand-in below composes optional filters in the same way.

JS Stand-in: optional filters and sorting

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Nested galleries and performance

A gallery inside a gallery is possible but expensive: each outer row runs the inner query. Prefer a single gallery with grouped data, or a flat table with a "group header" row. Keep templates simple:

  • Use few controls per row; images are costly.
  • Avoid LookUp against a data source in every row; join data once into a collection or use relationship columns.
  • Set a modest data row limit and paginate with a "Load more" pattern.

Common mistakes

  • Reading ThisItem outside the gallery template.
  • Forgetting .Selected is a record, not text.
  • Placing Patch inside Items (which must not change data).
  • Using galX.AllItems when the gallery hides rows with a filter in a control; it returns what the gallery shows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ThisItem refer to inside a gallery template?

    The template is repeated for each record and ThisItem is that row's record.
  2. 2. Which property gives the record the user tapped?

    Gallery.Selected returns the selected record.
  3. 3. How do you total a calculated value across every row shown by a gallery?

    AllItems exposes the shown table to aggregate functions.
  4. 4. Which function applies an action to every record in a table?

    ForAll runs a formula per record, typically with Patch in a behavior property.

Summary

  • A gallery repeats a template per record of Items; ThisItem is the current record.
  • Selected is the chosen record; Default sets the starting selection.
  • Use Filter, SortByColumns and Search in Items, and Sum/CountRows on AllItems.
  • Keep templates light; avoid nested galleries and per-row lookups.

Next: Forms and validation