Saturday, December 19, 2009

How to use color in a grid

How to color records in a grid is a common user request that can be found on the web.


Actually, this is quite simple to achieve in Ax, with only a small modification. The trick is to override the displayOption method on the form's datasource.



Non-overriden, it looks like this:



public void displayOption(Common _record, FormRowDisplayOption _options)
{
super(_record, _options);
}



By using the options object, an object of class FormRowDisplayOption, you can set the background color.



_options.backColor(myColor);





You can use the record information from _record to set any background you want, where each row in the grid can have a different background color.


For that reason, it may be better to change it's type to the specific table used in the datasource.


Like this:





public void displayOption(CustTable _CustTable,FormRowDisplayOption _options)


{ int myColor=WinApi::RGB2int(50,255,50);
;

if(_CustTable.Currency=='EUR')


_options.backColor(myColor);



super(_CustTable, _options);
}



In the example above, we color code our customers in the form CustTable. (Remember: Changes go in the methods of the form's datasource.) Customers with currency EUR get a green color.


The result looks like this:





By using colors, you can enhance the visual representation of your data. Making it easier for users to grab their attention, focusing them on certain data. The data gets processed faster.

Just make sure you don't overdo it, making it hurt your users eyes ;-)

5 comments:

  1. Clear post, thanks!

    What if it's possible to change the currency in the grid? What method should I call to update the color?

    Regards!

    ReplyDelete
  2. Hi Rob,

    Good question!
    I've played a bit with it, and this is what I would do. Override the modified method of your Currency field (on the datasource) with something like this:

    public void modified()
    { CustAccount lCustAccount=CustTable.AccountNum;
    ;
    if(lCustAccount)
    {
    super();

    element.lock();

    CustTable.update();

    CustTable_ds.executeQuery();
    CustTable_ds.findValue(fieldnum(CustTable,AccountNum),lCustAccount);
    element.unLock();
    }
    }

    This will trigger the displayOption method.
    Not ideal from performance point of view, but it works.

    ReplyDelete
    Replies
    1. I would like to thank your Willy help.
      Your example worked perfeitamento in case I'm doing.

      Thank you very much.

      Delete
  3. Hi Willy,

    Your option should work, but Vanya had a better one. You can have a look at his blogpost: http://kashperuk.blogspot.com/2010/03/tutorial-reread-refresh-research.html

    Regards,

    Rob

    ReplyDelete
  4. Source: http://kashperuk.blogspot.com/2010/03/tutorial-reread-refresh-research.html

    The research method starting with AX 2009 accepts an optional boolean argument _retainPosition. If you call research(true), the cursor position in the grid will be preserved after the data has been refreshed. This is an extremely useful addition, which solves most of the problems with cursor positioning (findRecord method is the alternative, but this method is very slow).

    ReplyDelete