Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to add the datasource, and its fields dynamically on a form?

(0) ShareShare
ReportReport
Posted on by

so i have one scenario, where I need to reflect all the fields of a table to another form which doesn't have any datasource and fields. It totally depend on the user. If he add x data values then x fields should be reflected to new form . 

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    Hi Nikolas,

    Thanks for your reply. Will do so.

  • Verified answer
    nmaenpaa Profile Picture
    101,156 Moderator on at
    RE: How to add the datasource, and its fields dynamically on a form?

    Hi Harshal,

    The sample code is just a static method which you can run from anywhere. And you can also put it anywhere.

    The code will create a form dynamically. You should put the code in the place where you want to trigger the creation of the form. You can point an action menu item to the class that has this method, and then place this menu item in places where you want to trigger the creation/launching of this dynamic form.

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    Can you please tell where should this be written?

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    I would like to ask in which method should I add this code? Should i add it in  init method of the form where I will be showing fields?

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    thanks for the code, but by this are we not creating the new form via code?/

  • Verified answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: How to add the datasource, and its fields dynamically on a form?

    Below I have shared a code showing how to add unbounded fields to form design. I just added Int64 and string fields but other types can be added with similar logic. The field adding part can be converted into a method as in the other example, and you can send the type and orher values from the outside.

    static void Dmr_ES_RunTimeFormDesign(Args _args)
    {
        Args                    args;
        Form                    form;
        FormRun                 formRun;
        FormBuildDesign         formBuildDesign;
        FormBuildStringControl  formBuildStringControl;
        FormBuildInt64Control   formBuildInt64Control;
        FormBuildTabControl     formBuildTabControl;
        FormBuildTabPageControl formBuildTabPageControl;  
        ;
        
        // Create the form header.
        form = new Form();
        
        // Create the form design.
        formBuildDesign = form.addDesign("Design");
        formBuildDesign.caption("myForm");
        
        // Add tabbed page controls, a grid control, and string controls.
        formBuildTabControl = formBuildDesign.addControl(FormControlType::Tab, "Overview");
            
        formBuildTabPageControl = formBuildTabControl.addControl(FormControlType::TabPage,"Details");
        formBuildTabPageControl.caption("Details");
       
        formBuildInt64Control = formBuildTabPageControl.addControl(FormControlType::Int64, "MyInt64UnboundedField");
        formBuildInt64Control.label("MyInt64UnboundedField");
        formBuildInt64Control.helpText("MyInt64UnboundedField");
        formBuildInt64Control.allowEdit(true);
        formBuildInt64Control.value(1234);
        
        formBuildStringControl = formBuildTabPageControl.addControl(FormControlType::String, "MyStringUnboundedField");
        formBuildStringControl.label("MyStringUnboundedField");
        formBuildStringControl.helpText("MyStringUnboundedField");
        formBuildStringControl.allowEdit(true);
        formBuildStringControl.text("My string Value");
        
        args = new Args();
        args.object(form);
        
        // Create the run-time form.
        formRun = classfactory.formRunClass(args);
        
        formRun.run();
        formRun.detach();
    
    }

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    3771.pic.PNG

    Now this is an example- these fields can be infinite number. As soon as user saves the field, these fields should be reflected to other form according to their type. Hope you get it.

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: How to add the datasource, and its fields dynamically on a form?

    It's not exactly dynamic in the example you showed. There is a table and form in the background. Only the form has no design. It creates a new design with the fields of the table.

    With a similar structure, you can add table-independent unbounded (without datasource and datafield value) controllers. You can assign an initial value. But in this case, it is not possible to have a grid-like structure. Each control can hold only one value.

  • Community Member Profile Picture
    on at
    RE: How to add the datasource, and its fields dynamically on a form?

    That table is still dynamic. I want to add the field according to the users preference. suppose the user want 3 text type fields then first i need to create 3 string data type fields. Still cant get it , how should i do it.

  • ergun sahin Profile Picture
    8,816 Moderator on at
    RE: How to add the datasource, and its fields dynamically on a form?

    Of course, you can create a form with code and run it in runtime, but I'm not sure if you really need such a thing. In the first form there is a table where you get the values from the user. Why not use the same table.

    Also validateWrite is not a place where we would want to add this kind of code. If there is no very special situation, write it under a button as in the example.

    Lastly, I can't see any serious errors in the example, but I have to try it to be sure (I may not have time during the day).

    If you share with us the error you encounter while using it, we will try to help.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans