Skip to main content

Helper Extension Usage for DataGrids on the Flow Side

This documentation describes how to create and use the helper extension method, which is used on the Flow side to create a maintainable structure for the invocation of DataGrid elements.

Problem Definition

On the Flow side, access to the cells of the DataGrid's elements is usually performed with index values. However, this method moves away from a sustainable structure due to changes to the DataGrid. Therefore, we can create a more sustainable method by providing access to cells by name.

Solution: Using the Extension Method

We'll create an extension method to provide access to DataGrid cells by name. In this way, we can make access to cells more readable and sustainable.

Steps

  1. Creating the Extensions Folder:**

In the main folder structure, create a folder named 'Extensions'.

  1. Creating the GridDataRowExtension.cs File:

Create a file named 'GridDataRowExtension.cs' in the 'Extensions' folder and add the following class:

'''csharp using Bimser.Synergy.ServiceAPI.Models.Form; using System.Linq; using System; using Newtonsoft.Json;

namespace ExampleProject1.Extensions { public static class GridDataRowExtension { public static GridDataRowCell GetCellByName(this GridDataRow gridDataRow, string name) { var cell = gridDataRow.Cells.FirstOrDefault(x => x.Name == name); if (cell is null) throw new Exception($"GridDataRowExtension.Error, A cell named {name} could not be found!"); return cell; } } }

```
  1. Access to Extension on the Flow Side:

To be able to access this extension on the flow side, add the required using statement:

'''csharp using ExampleProject1.Extensions;

```
  1. Use of the Extension Method:

With this extension method, you can access DataGrid cells by name. For example:

'''csharp var approverList = GridData.FromControl(Document1.Controls["dgApprovers"]); var currentApprover = approverList.Rows.First(x => x.GetCellByName("APPROVESTATUS"). Value.ToString() == "Unconfirmed");

```

Comparison

My Call Using Extension Method:

'''csharp var approverList = GridData.FromControl(Document1.Controls["dgApprovers"]); var currentApprover = approverList.Rows.First(x => x.GetCellByName("APPROVESTATUS"). Value.ToString() == "Unconfirmed");


**My Call Without Using Extension Method:**

'''csharp
var approverList = GridData.FromControl(Document1.Controls["dgApprovers"]);
var currentApprover = approverList.Rows.First(x =```>``` x.Cells[3]. Value.ToString() == "Not confirmed!");

Conclusion

You can create a more sustainable and readable structure by accessing DataGrid cells with the names of the cells instead of accessing them with an index. If you use this process frequently, you can make your code more modular and manageable by using the extension method.