Consultor Eletrônico



Kbase 18701: Apptivity - How to Calculate Value in Grid Column on Event
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   3/25/1999
SUMMARY:

The simplest way to include a calculated value as a column in a
databound abGridView control is to do the calculation in the SELECT
statement so that the value is returned to the query. This will
populate the grid column upon connecting to the data source.

If instead, you wish to calculate the values for that column after,
and possibly based upon, user action, you can do this by creating a
computed value Data Object, binding a grid column to that Data Object,
and calculating the value for all rows in the event handler for that
action. The code below illustrates such a case.

SOLUTION:

The following example uses the Suppliers table of the Northwind
database. This code calculates an integer value for each row of the
databound grid, based on the SupplierID column of the Suppliers
table. This is a very simplified case, intended for illustration only.

The DataObject computed value:

class cvlInteger extends abCalculator
{
public Object recalc ( abEnv theApp )
{
Integer retVal = null; //Initialize retVal here
//Add your code here
allRowsComputed(); //Indicates that all row
//values have been computed
//End of your code
return retVal;
}
}

Code that executes the computation, in this case, in an action event
handler for a button:

public void OnActionbutton1 (Object arg)
{
//TODO: Add your handler code here
//
abDataSource ds = theApp().getDataSource("ds_suppliers");
try {
while (!ds.isReady()) Thread.currentThread().sleep(100);
while (ds.getRecordSet().rowCount() == -1)
Thread.currentThread().sleep(100);

}
catch (Exception e) { e.printStackTrace(); }

try {
computeForAll(ds);
}
catch (Exception e) { System.out.println("Error" + e);}
return;
}


Method called in the above code:

public void computeForAll(abDataSource ads)
{
int liRows = ads.getRecordSet().rowCount();
Integer sID = null;

try {
abCalcValue lcvlInteger =
(abCalcValue)theApp().getGlobal("cvlInteger");

for (int li = 1; li <= liRows; li++)
{
sID = (Integer)
(ads.getRecordSet().getColumn("SupplierID").getValue(li));

Integer newVal = new Integer (sID.intValue() + 1);
lcvlInteger.recordComputedValue(newVal,li);
}

}

catch (Exception e) { e.printStackTrace(); }
}

References To Written Documentation:

Apptivity Developer's Guide
Progress Knowledge Base Solutions:
17651, "Apptivity: Display Calculated Database Fields in Grid"
18638, "Apptivity - Computed Value Data Object as Grid Column"