link a query to a datagrid on runtime.


In Dynamics AX 2009.

How can I create my own query by code, and link it to an existing grid of a form?

Need sample please.

(i.e salesLine that linked to sales table etc…)

Thanks :slight_smile:


just try this example

Example: Building a query in X++

The following code demonstrates how a query is built from scratch in X++. This

query returns all sales orders, from the SalesTable, for customer ‘4008’, sorted

by the SalesId.

Query query;

QueryRun queryRun;

QueryBuildDataSource qbds;

QueryBuildRange qbr;

SalesTable SalesTable;


query = new Query();

//this line attaches a table to the qbds data source object

qbds = query.addDataSource(TableNum (SalesTable));

//this line attaches a range to the ‘SalesTable’ //data

source, the range is the CustAccount

qbr = qbds.addRange(FieldNum (SalesTable,CustAccount));

// The range is set to ‘2001’

qbr.value (‘2001’);

// The query will sort by sales id

qbds.addSortField (FieldNum(SalesTable,SalesId));

// The queryRun object is instantiated using the query

queryRun = new QueryRun(query);

// The queryRun object loops through all records returned

while (


// The current record is assigned to the salesTable


SalesTable = queryRun.get(tableNum(SalesTable));

print SalesTable.SalesId;



writing the query for two tables is as below. But If u can explain me ur second requirement, it will help me to specify solution.

Query query;
QueryRun queryRun;
QueryBuildDataSource qbdsSalesTable, qbdsSalesLine;
QueryBuildRange qbrSalesTable, qbrSalesLine;
SalesTable SalesTable;
SalesLine salesLine;

query = new Query();

qbdsSalesTable = query.addDataSource(TableNum (SalesTable));
qbrSalesTable = qbdsSalesTable.addRange(FieldNum (SalesTable,SalesId));

qbdsSalesLine = qbdsSalesTable.addDataSource(TableNum (SalesLine));
qbrSalesLine = qbdsSalesLine.addRange(FieldNum (SalesLine,SalesId));

queryRun = new QueryRun(query);
SalesTable = queryRun.get(TableNum(SalesTable));
salesLine = queryRun.get(TableNum(salesLine));
print salesTable.SalesId;
print SalesLine.SalesId;

Hope it will help you…[:D]

That’s OK - but almost.

I need to connect the query to a grid on form (The grid is connected to FromDataSource, but how can I do that manually).

Thanks :slight_smile: