# Writing back the data

As we make changes into the dataset in the grid, Power Grid will output an string that can be found on **onUpdatedItems** property.

For instance if we change (on the grid) **Category** field to **Planning** (items 6 and 7) the output is going to be:

```xml
6</>Category</>Planning</Row>7</>Category</>Planning</Row>
```

The next step is to transform this string into a collection that can be used to write back to our data source.

### Using onUpdatedItems to change the data

To do so let's use the formula below. This will split the string into a table using the tag \</row> as separator.&#x20;

```csharp
Clear(updatedRowsCol);
Clear(updatedItemsCol);


// 1.  Parsing changed rows into a collection

ForAll(
    Split(
        LowcoderaDataGrid1.onUpdatedItems,
        "</Row>"
    ),
    Collect(
        updatedRowsCol,
        Result
    )
);

```

The result is a collection like this:

![](https://3220951636-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnUfK4SRGZUB01uvHGeDB%2Fuploads%2Fbvm60AajTsnEnfo32F34%2Fimage.png?alt=media\&token=e28e113c-0fb7-46b3-9db4-ffff469180db)

Now let's get the field, ID and value organized in a tabular way. For it we are going to use the Power FX formula as below:

```csharp
ForAll(
    updatedRowsCol,
    Collect(
        updatedItemsCol,
        {
            id: First(
                Split(
                    ThisRecord.Value,
                    "</>"
                )
            ).Result,
            fieldName: First(
                LastN(
                    Split(
                        ThisRecord.Value,
                        "</>"
                    ),
                    2
                )
            ).Result,
            value: Last(
                LastN(
                    Split(
                        ThisRecord.Value,
                        "</>"
                    ),
                    2
                )
            ).Result
        }
    )
);
```

![](https://3220951636-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnUfK4SRGZUB01uvHGeDB%2Fuploads%2FWTaVT2G7R87fVxMCFiIv%2Fimage.png?alt=media\&token=202dd9ca-2136-4405-903b-70fb924473c0)

Finally we can loop through the data while changing our original data source.

```csharp
ForAll(
    updatedItemsCol As UpdatedItems,
    Switch(
        UpdatedItems.fieldName,
        "Title",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Title: UpdatedItems.value}
        ),
        "Description",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Description: UpdatedItems.value}
        ),
        "Category",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Category: UpdatedItems.value}
        ),
        "Progress",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Progress: UpdatedItems.value}
        ),
        "Priority",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {Priority: UpdatedItems.value}
        ),
         "StartDate",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {StartDate: DateValue(UpdatedItems.value)}
        ),
             "DueDate",
        Patch(
            colWorkProgressTracker,
            LookUp(
                colWorkProgressTracker,
                ID = Value(UpdatedItems.id)
            ),
            {DueDate: DateValue(UpdatedItems.value)}
        )
    )
)
```

{% hint style="info" %}
Note that we can define which fields will receive changes by placing or removing on the switch statement
{% endhint %}

Now, create a button for saving back the data

```csharp
Patch(
    WorkProgressTracker,
    ShowColumns(
        colWorkProgressTracker,
        "ID",
        "Title",
        "Description",
        "Category",
        "Progress",
        "Priority",
        "StartDate",
        "DueDate"
    )
)
```
