Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Coloring grid colomn on a form

Posted on by 142

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?

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Coloring grid colomn on a form

    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.

  • Hidden developer Profile Picture
    Hidden developer 142 on at
    RE: Coloring grid colomn on a form

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

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Coloring grid colomn on a form

    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
    Hidden developer 142 on at
    RE: Coloring grid colomn on a form

    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.

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Coloring grid colomn on a form

    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
    Hidden developer 142 on at
    RE: Coloring grid colomn on a form

    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
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Coloring grid colomn on a form

    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.)

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans