Tuesday, July 21, 2015

Change Text/BackGround Color Based Off Cell Value Within a Grid


In some cases within AX you may want to treat data being displayed on the form as a quick report and within reports sometimes it’s nice to be able to change a cells text color or back ground color based on the cells value wither its a stored value or display value shouldn’t matter. Below will show you how to change a single cell based on its value or change the entire row. Since all of this code gets executed at the same location is it almost the same code it just has subtle differences. The examples shown will show you the difference between changing a cell text color, cell background color and row background color which are all based off the display field/method InventQualityOrderTable.numberOfDueDays()

 
Note: the method qualityOrder.NumberOfDaysDueDate() will return either a positive number, negative number or the text "No Due Date"
 

Example 1. Change Cell Value Text Color Based on Display Field.

Step 1. Set AutoDeclaration on the field control to true (in the example shown it’s called DifferenceDueDate)
Step 2. Go to the forms datasource and create the override method “displayOption”
Step 3. Enter the following code

 

 

public void displayOption(Common _record, FormRowDisplayOption _options)
{

    InventQualityOrderTable qualityOrder;
    str 15 numberOfDueDays;

 
    qualityOrder = _record;

 
    //get the number of days between due date/complete or now/duedate
    numberOfDueDays = qualityOrder.NumberOfDaysDueDate();

    //only do the compare if we have a number/due date
    if(CustomMethods::isNumeric(numberOfDueDays))
    {

        //if the value is bigger than 0 than it should be a miss (red)
        if(str2num(numberOfDueDays) > 0)
        {
            _options.affectedElementsByControl(DifferenceDueDate.id());

            //red
            _options.textColor(WinAPI::RGB2int(255,0,0));
        }
        else if(str2num(numberOfDueDays) <= 0)
        {
            //if the value is less than or equal to 0 than the due date was meet and the text should be green
            _options.affectedElementsByControl(DifferenceDueDate.id());

            //green
            _options.textColor(WinAPI::RGB2int(0,204,0));

        }
    }

    super(_record, _options);
}

 

 

Example 2. Change Cell Value Back Color Based on Display Field.

Step 1. Set AutoDeclaration on the field control to true (in the example shown it’s called DifferenceDueDate)
Step 2. Go to the forms datasource and create the override method “displayOption”
Step 3. Enter the following code

 

public void displayOption(Common _record, FormRowDisplayOption _options)
{

    InventQualityOrderTable qualityOrder;
    str 15 numberOfDueDays;

 
    qualityOrder = _record;

 
    //get the number of days between due date/complete or now/duedate
    numberOfDueDays = qualityOrder.NumberOfDaysDueDate();

    //only do the compare if we have a number/due date
    if(CustomMethods::isNumeric(numberOfDueDays))
    {

        //if the value is bigger than 0 than it should be a miss (red)
        if(str2num(numberOfDueDays) > 0)
        {
            _options.affectedElementsByControl(DifferenceDueDate.id());

            //red
            _options.backColor(WinAPI::RGB2int(255,0,0));
        }
        else if(str2num(numberOfDueDays) <= 0)
        {

            //if the value is less than or equal to 0 than the due date was meet and the text should be green
            _options.affectedElementsByControl(DifferenceDueDate.id());

            //green
            _options.backColor(WinAPI::RGB2int(0,204,0));
        }
    }

    super(_record, _options);
}

 

 

 

Example 3. Change Cell Value Back Color Based on Display Field.

Step 1. Go to the forms datasource and create the override method “displayOption”
Step 2. Enter the following code

 
public void displayOption(Common _record, FormRowDisplayOption _options)
{

    InventQualityOrderTable qualityOrder;
    str 15 numberOfDueDays;

    qualityOrder = _record;

 

    //get the number of days between due date/complete or now/duedate
    numberOfDueDays = qualityOrder.NumberOfDaysDueDate();

    //only do the compare if we have a number/due date
    if(CustomMethods::isNumeric(numberOfDueDays))
    {

        //if the value is bigger than 0 then it should be a miss (red)
        if(str2num(numberOfDueDays) > 0)
        {
            //red
            _options.backColor(WinAPI::RGB2int(255,0,0));
        }

        else if(str2num(numberOfDueDays) <= 0)
        {

            //if the value is less than or equal to 0 than the due date was meet and the text should be green

            //green
            _options.backColor(WinAPI::RGB2int(0,204,0));
        }
    }

    super(_record, _options);
}

 

 

Conclusion: The difference between setting an entire rows color and setting a cell/field value is only different by the method _options.affectedElementsByControl(<field control>.id()); which limits what fields the next color change affects.

 The only difference between setting a text color and a back ground color is the property textColor vs backColor()