Generate Grid Columns Dynamically

Hi All,

Today I want to show you an example how you can generate grid columns dynamically.
Sometime it is really important for dynamic inquiry report, where you want to show data that may be changed due to some other configurations.

generate grid columns dynamically in acumatica

To add columns dynamically you should follow this plan:

  1. Create a separate method that will generate columns.
  2. Call method create above from the graph constructor. Please not that if you put method into any event you need to make sure that you generation columns only once. Also you logic assume that columns may be changes if user select some other values on the same screen you need to make sure that you delete previously generated columns.
  3. On the columns go throught all new columns and do following:
    1. Add new Field to the PXCached
    2. Subscribe 2 events for FieldSeleting and FieldUpdating. These are most important events that are interacting with user interface. On the FieldSeleting you need to provide value and the configuration how this value should be displayed. On FieldUpdating you need to check, how system updated the value back to cache.

Here you can see a code snippet for columns generation:

protected void GenerateColumns(PXCache sender, List<String> columns)
{
       foreach (String column in columns)
       {
              String key = column;
              String name = column;

              if (!sender.Fields.Contains(key))
              {
                      sender.Fields.Add(key);
                      this.FieldSelecting.AddHandler("Partners", key, 
                             (PXCache cache, PXFieldSelectingEventArgsargs) =>
                      {
                      });
                      this.FieldUpdating.AddHandler("Partners", key,
                             (PXCache cache, PXFieldUpdatingEventArgs args) =>
                      {
                      });
              }
       }
}

Note that on FieldSelecitng you should generate and provide PXFieldState and Value. PXFieldState is a special class that is generated based on multiple properties of different attributes during FieldSelecitng event (For example PXFieldState.DisplayName will be set from PXUIFieldAttribute.DisplayName). You can copy PXFieldState from other field using PXCache.GetStateExt or you can construct it by your own.

Also note that system may call FieldSelecitng several time for each field with row=null and row=DAC. This is required to generate base column definition first and each separate cell with specific data there.

Also you do not forget to change AutoGenerateColumns PXGrid properties in the page aspx file, to enable auto generation of columns.

<px:PXGrid ID="gridPartners" RestrictFields="True" AutoGenerateColumns="Append">

Full code snippet:

Have a nice development!

1 Reply to “Generate Grid Columns Dynamically”

Leave a Reply

Your email address will not be published. Required fields are marked *