link a query to a datagrid on runtime.

Hello.

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:

hi,

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 (queryRun.next())

{

// The current record is assigned to the salesTable

variable

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

print SalesTable.SalesId;

}

Hi,

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));
qbrSalesTable.value(‘SO001’);

qbdsSalesLine = qbdsSalesTable.addDataSource(TableNum (SalesLine));
qbdsSalesLine.relations(True);
qbrSalesLine = qbdsSalesLine.addRange(FieldNum (SalesLine,SalesId));
qbrSalesLine.value(‘SO001’);

queryRun = new QueryRun(query);
while(queryRun.next())
{
SalesTable = queryRun.get(TableNum(SalesTable));
salesLine = queryRun.get(TableNum(salesLine));
if(queryRun.changed(TableNum(SalesTable)))
{
print salesTable.SalesId;
}
if(queryRun.changed(TableNum(SalesLine)))
{
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: