web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Coloring grid colomn on a form

(0) ShareShare
ReportReport
Posted on by 144

Hello,

I have a form with a grid and I need to color 2 columns of the grid depending on the values of the other 2 different columns.

Do you happen to bump into something similar?

I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Isn't it about the same thing as your thread Color the grid column depending on other column? If so, let me delete this one. It's better to have the discussion at one place.

    Did you see my response there?

    Also, the other thread is in Dynamics 365 Finance forum - doesn't this thread belong there too? (Assuming that it's not a duplicate.)

  • Hidden developer Profile Picture
    144 on at

    Yes, it is the same task but a bit different problem. I solved the previous one as you recommended. But now I encountered that whenever I try to color the first column and then the second one then both columns have colors based on the second column logic.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    It seems that you have code that doesn't work as expect. Please share it with us. (Use Insert > Code in the rich formatting view to paste source code).

    By the way, you didn't respond to my questions about forums. Your previous question was in a D365FO forum and this one is in the AX forum. Doesn't it actually belong to a D365FO forum too?

  • Hidden developer Profile Picture
    144 on at

    public void displayOption(Common _record, FormRowDisplayOption _options)
    {
        MyTable table;
        table = _record;
        _options.affectedElementsByControl(MyControl_1.id());
    
        switch(table.MyField_1)
        {
            case value_1:
                _options.backColor(WinAPI::RGB2int(#PastelRed));
                break;
            case value_2:
                _options.backColor(WinAPI::RGB2int(#PastelYellow));
                break;
        }
    
        //_options.clear();
        _options.affectedElementsByControl(MyControl_2.id());
    
        switch(table.MyField_2)
        {
            case value_3:
                _options.backColor(WinAPI::RGB2int(#PastelRed));
                break;
            case value_4:
                _options.backColor(WinAPI::RGB2int(#PastelYellow));
                break;
        }
    
        super(_record, _options);
    }

    What is expected: MyControl_1, MyControl_2 colors are independent and are determined by MyField_1 and MyField_2 relatively.

    What happened: the second switch rewrites the first switch and as a result both controls colors are based on MyField_2.

  • Suggested answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    The first call of affectedElementsByControl() has no effect, because you always overwrite the value by the second affectedElementsByControl() call.

    You will need to set the control only when the condition is met:

    public void displayOption(Common _record, FormRowDisplayOption _options)
    {
        MyTable table = _record;
    
        switch(table.MyField_1)
        {
            case value_1:
                _options.backColor(WinAPI::RGB2int(#PastelRed));
    			_options.affectedElementsByControl(MyControl_1.id());
                break;
            case value_2:
                _options.backColor(WinAPI::RGB2int(#PastelYellow));
    			_options.affectedElementsByControl(MyControl_1.id());
                break;
        }
    
        switch(table.MyField_2)
        {
            case value_3:
                _options.backColor(WinAPI::RGB2int(#PastelRed));
    			_options.affectedElementsByControl(MyControl_2.id());
                break;
            case value_4:
                _options.backColor(WinAPI::RGB2int(#PastelYellow));
    			_options.affectedElementsByControl(MyControl_2.id());
                break;
        }
    
        super(_record, _options);
    }

    I'll improve the situation, but your expectation isn't correct anyway - the controls won't be completely independent. You aren't setting a colour for a column - you're setting a colour for a record (and choosing which controls it'll apply to). If conditions for both fields are met, colour will be set according to MyField_2, because it'll overwrite backColor set by MyField_1.

  • Hidden developer Profile Picture
    144 on at

    Yes, it is not exactly what I need since the usual case is that the record met one of the conditionals in each switch.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    If this isn't what you want, you've chosen a wrong solution for your business problem.

    I don't know your business requirement, therefore I can't propose a better design. But there surely are other approaches, such showing icons (through display methods) instead of changing background colors, showing colors in details instead of a grid, or using a report instead of a form.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans