Wednesday, December 6, 2017

AX - How change Grid cells colors (conditional formatting)

When you need to change grid cells color you must override form DataSource displayOption() method.


This method has two parameters:
  • First is record for every row in grid.
  • Second is used for color change.
Here is applied change only for grid`s column ProductivityPercent.
public void displayOption(Common _record, FormRowDisplayOption _options)
{

    DM_ProductivityTable dm_productivityTable;
    real iPercent;

    /* record for every row (currently drawn row), get percent from it */

    dm_productivityTable = _record;

    iPercent = dm_productivityTable.ProductivityPercent;

    /* conditions */

    if ( ( iPercent > 0 ) && ( iPercent < 75 ) ) {
        _options.backColor( WinAPI::RGB2int( 153, 31, 0 ) );
        _options.textColor( WinAPI::RGB2int( 255, 255, 255 ) ) ;
        _options.affectedElementsByControl( DM_ProductivityTable_ProductivityPercent.id() );
    }
    if ( ( iPercent >= 75 ) && ( iPercent < 100 ) ) {
        _options.backColor( WinAPI::RGB2int( 255, 92, 51 ) );
        _options.textColor( WinAPI::RGB2int( 255, 255, 255 ) ) ;
        _options.affectedElementsByControl( DM_ProductivityTable_ProductivityPercent.id() );
    }
    if ( ( iPercent >= 100 ) && ( iPercent < 120 ) ) {
        _options.backColor( WinAPI::RGB2int( 0, 230, 0 ) );
        //_options.textColor( WinAPI::RGB2int( 255, 255, 255 ) ) ;
        _options.affectedElementsByControl( DM_ProductivityTable_ProductivityPercent.id() );
    }
    if ( iPercent >= 120 ) {
        _options.backColor( WinAPI::RGB2int( 0, 128, 0 ) );
        _options.textColor( WinAPI::RGB2int( 255, 255, 255 ) ) ;
        _options.affectedElementsByControl( DM_ProductivityTable_ProductivityPercent.id() );
    }

    /* call */

    super( _record, _options );
}

No comments:

Post a Comment