Why exactly do you want complicate your life and waste your time? You can achieve the same thing using the designer in a fraction of time that you would need to write all the code by hand. You seem to already know how the form should look like, so you can do it at design-time.
Anyway, such datasource must a link, and it should be the delayed link for performance reasons. AX already contains such a form with sales orders and lines (called SalesTable), so you always look there if in doubt.