Blog

Editable Gallery - How to set a control value dynamically when a user changes the value of another control

Learn how to set control values dynamically in a gallery without unintentionally changing all rows in the gallery.

A common requirement when building apps is to add an editable grid to provide Excel-type editing. To implement this functionality, we can add a gallery control with editable controls. We can then save the contents by referencing the AllItems property of the gallery control and calling the ForAll/Patch functions.

This post focuses on a specific challenge that app builders often struggle with - that is, how to set a control value for a specific record when the user changes another value in that record. A common problem when using variables is that all control values for all items in the gallery are changed.

This post walks through a technique to carry out this task without using variables.

Demonstration

To demonstrate this technique, let's take an example of a gallery control populated with records from a list of SharePoint Issue records. The columns in this list are shown beneath.


Each record contains a status field. Our example requirement is as follows. When a user sets the status to ‘In Progress’, the ‘Target End Date’ value should be set to today’s date plus 7 days.

To set up the gallery, we perform the initial following steps:

  1. Set the Items property to the Issue datasource.
  2. Inside the gallery, add a combo box for the status field (cboStatus). Status is a SharePoint choice field so we set the Items property to ThisItem.Status
  3.  Inside the gallery, add a date picker for the target end date (dpTargetEndDate).



For the 'target end date' date picker control, set the default property of the control to the following formula.

If(cboIssueStatus.Selected.Value <> ThisItem.IssueStatus.Value, 
DateAdd(Today(), 7, TimeUnit.Days),
ThisItem.TargetCloseDate
)
This formula, by default, sets the value of the 'target end date' date picker to the existing record value. However, if the value of the status combo box deviates from the status of the existing record, it applies today's date plus a week.

The remaining step is to call the Reset function in the OnChange property of the status dropdown.

Reset(dpTargetEndDate)

When we run the app, the gallery functionality will behave as expected. When a user changes the status to 'in progress', the  'target end date' date picker will reset to today's date plus 7 days.

Conclusion

By following this technique, we can dynamically set control values in Power Apps without affecting all rows in the gallery. This approach ensures that only the intended record is updated.