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.
To add columns dynamically you should follow this plan:
- Create a separate method that will generate columns.
- 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.
- On the columns go throught all new columns and do following:
- Add new Field to the PXCached
- 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.
protected void GenerateColumns(PXCache sender, List<String> columns)
foreach (String column in columns)
String key = column;
String name = column;
(PXCache cache, PXFieldSelectingEventArgs args) =>
(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!