Kbase P171226: How to find a row in an UltraGrid based on a column value
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  8/12/2010 |
|
Status: Unverified
GOAL:
How to find a row in an UltraGrid based on a column value
GOAL:
How to implement search functionality in an UltraGrid
GOAL:
How to implement lookup functionality in an UltraGrid
FACT(s) (Environment):
Windows
OpenEdge 10.2x
FIX:
There is no search functionality built into the UltraGrid. The ABL application programmer can implement this functionality using a temp-table. Define a temp-table with fields for the column(s) to be searched on and the row number of the UltraGrid. This temp-table can be indexed or not, according to the standard criteria for efficient use of indexes in OpenEdge. When the UltraGrid is populated from its data source, populate the temp-table as well; each row number is added in sequence as the data source record is loaded into the UltraGrid row. (Remember that UltraGrid rows are numbered starting with zero, but most ABL objects, including queries, are numbered starting with one.) This temp-table can then be used to find the row number of the desired record in the UltraGrid, and activate or otherwise manipulate that row. The application programmer is responsible for keeping the temp-table synchronized with the UltraGrid if the UltraGrid is updatable.
The following code snippets illustrate how to search an UltraGrid by customer number. The data source of the UltraGrid is a query on the Sports2000 Customer table.
To define the temp-table:
DEFINE PRIVATE TEMP-TABLE ttCustNumToRow NO-UNDO
FIELD CustNum AS INTEGER
FIELD RowIndex AS INTEGER
INDEX idxCustNum IS PRIMARY CustNum
.
To populate the temp-table:
CREATE QUERY hCustomerQuery.
hCustomerQuery:SET-BUFFERS(BUFFER Customer:handle).
cCustomerQueryString = "PRESELECT EACH Customer NO-LOCK".
hCustomerQuery:QUERY-PREPARE(cCustomerQueryString).
hCustomerQuery:QUERY-OPEN().
bsCustomer:handle = hCustomerQuery.
/* Empty the lookup temp-table of any existing values */
EMPTY TEMP-TABLE ttCustNumToRow.
/* Get handles to the default query buffer and its CustNum field */
hBuffer = phCustomerQuery:GET-BUFFER-HANDLE ().
hCustNum = hBuffer:BUFFER-FIELD ('CustNum').
/* For each query row, create a record in the lookup temp-table. Subtract
one from CURRENT-RESULT-ROW to get the row index because ABL query row
indexes are one-based, but UltraGridRow indexes are zero-based.
*/
phCustomerQuery:GET-FIRST ().
DO WHILE NOT phCustomerQuery:QUERY-OFF-END:
CREATE ttCustNumToRow.
ASSIGN
ttCustNumToRow.CustNum = hCustNum:BUFFER-VALUE ()
ttCustNumToRow.RowIndex = phCustomerQuery:CURRENT-RESULT-ROW - 1
.
phCustomerQuery:GET-NEXT().
END.
To locate the desired row based on the customer number entered into a text box:
FIND FIRST ttCustNumToRow WHERE ttCustNumToRow.CustNum = INTEGER(textBoxCustNum:Text) NO-ERROR.
IF AVAILABLE ttCustNumToRow THEN
.DO:
ultraGridCustomer:ActiveRow:Selected = FALSE.
ultraGridCustomer:ActiveRow = ultraGridCustomer:Rows[ttCustNumToRow.RowIndex].
END.
ELSE
MESSAGE 'Customer Number "' textBoxCustNum:Text '" is not in grid' VIEW-AS ALERT-BOX..