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 :
Microsoft Dynamics AX (Archived)
Suggested Answer

Disable grid field based on other field value

(0) ShareShare
ReportReport
Posted on by

Hello:

I have a grid on a custom form with a FieldA I'd like to disable / not allow change if another FieldB on the same record is of a specific value. I tried code in the datasource's active method but it only works on the first record. I'd like to have all records loaded to on that grid to enable/disable FieldA if FieldB has a certain value. 

if (TBL.FieldB == NoYes::Yes)

TBL_ds.object(fieldNum(TBL, FieldA)).allowEdit(false);

else

TBL_ds.object(fieldNum(TBL, FieldA)).allowEdit(true);

Any help would be appreciated.

Thank you.

*This post is locked for comments

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,069 Super User 2025 Season 2 on at

    Hi DHeisman,

    You can try to use the coding on the leave() or leaveRecord() method of the datasource.

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

    active() method is indeed the right place, and your code looks all right too.

    Are you aware of that active() executes when you select a record? If you open the form, it executes for the first record, because it's selected (and it's the only record you can edit), if you switch to another record, active() executes for it, and so on.

    It's off topic, but I would refactor your code not to repeat the field selection. For example:

    boolean enableA = tbl.FieldB == NoYes::No;
    tbl_ds.object(fieldNum(TBL, FieldA)).allowEdit(enableA);
  • Community Member Profile Picture
    on at

    Hi Andre,

    Leave() and leaveRecord() seem to just do the opposite of what I was seeing before with active(). What I'd like to do is have all this logic run per record as the grid loads.

    Thank you.

  • Community Member Profile Picture
    on at

    Hi Martin,

    Yes, seems like active() is probably the wrong call. I tried init() which didn't appear to be the correct method call either.

    And, thank you for the suggestion on the code refactor. It certainly looks much cleaner and readable.

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

    No, active() is the right method and it's widely used for your kind of scenario. It just work differently than you thought.

    Allowing and disabling editing make no sense for other records than the selected one, because if you want to edit another record, you have to select it first, which is when active() triggers and runs your logic.

  • Community Member Profile Picture
    on at

    Hi Martin,

    That makes sense. Adding the code in the active() method does appear to check / set the fields I need. However, I'm seeing this odd reaction when I change the value of TBL.FieldB on a record. Once I do that, it resets all the enabled flag of all the records based on the changed record. There must be something I've overlooked.

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

    All flags on all records? What do you mean?

    So far we were talking about the property of of the data source field, which is only one. It doesn't matter whether you have zero or a million records; the form has a just single definition of the data source and its fields. The same is true for the grid column - it has just a single definition.

    Can elaborate what problem you have with the solution discussed above?

  • Community Member Profile Picture
    on at

    Hi Martin,

    Hopefully I can explain this clearly. With the code in place, once I open the form, the currently selected field would render with FieldA grayed out because FieldB for that record has a Yes value. As I click each record beneath, each would render with FieldB grayed out if FieldA for the current record is Yes. If I select a record where FieldA equals No, FieldB would not gray out. All this appears to work just as I needed.

    Here's where things look a bit odd. Imagine I have three records (Rec1, Rec2, Rec3). Rec1 and Rec3 have a FieldB value of Yes so FieldA for those two records are grayed out. Rec2's FieldB value is No, hence FieldA is not grayed out. If I select FieldB on Rec3 to set it to No (uncheck the box), then Rec1 and Rec2's FieldA both gray out. If I select Rec1 and Rec2, they both update and look OK. I close the form and reopen it. I select each record again, so now Rec2 and Rec3 have FieldB set to No while Rec1's FieldB is Yes. Now the only record with FieldA grayed out is Rec1. I click on Rec3's FieldB field to set to Yes (check the box), now Rec1's FieldA is no longer grayed out. When I click into the Rec1 record it will update and gray FieldA again.

    What appears to be happening is whichever record I'm updating by checking / unchecking FieldB applies it's logic across all the records. Again, I hope that makes sense as to the sequence of events.

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

    Again, there is no setup for individual rows. You're setting property of the grid column, which is obviously just one, therefore it always applies to all records.

    But it's not any problem, because your logic will always execute and enable or disable the field as needed, when users switch to the record they want to edit.

  • Community Member Profile Picture
    on at

    Thank you Martin. The behavior seems a bit erratic on the form but hopefully the users can live with it.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans