TDAMemDataTable: Advanced record filtering (Delphi)
Overview
TDAMemDataTable is a class that holds live data on the client application at runtime, to make it available to application code and visual controls. TDAMemDataTable can be filled from a remote source (typically a Data Abstract server) using a Remote Data Adapter component that coordinates the connection to the server; it maintains a local history of changes (Delta) and can apply those changes back to the database, again using the Remote Data Adapter.
TDAMemDataTable allows to do filtering in two ways:
Standard filtering
- Filter property allows to specify an conditional expression as a string. After specifying Filter value set Filtered property to True to activate the filter, e.g:
DAMemDataTable1.Filter := '(OrderID > 10) AND (OrderID < 30)';
DAMemDataTable1.Filtered := True;
- OnFilterRecord event handler allows to implement filtering as a Delphi code. After specifying OnFilterRecord value set Filtered property to True to activate the filter, e.g.:
procedure TForm1.DAMemDataTable1FilterRecord(DataTable: TDADataTable; var Accept: Boolean)
var
lValue: Integer;
begin
lValue := DataTable.FieldByName('OrderID').AsInteger;
Accept := (lValue > 10) or (lValue < 30);
end;
procedure TForm1.FilterClick(Sender: TObject);
begin
DAMemDataTable1.OnFilterRecord := DAMemDataTable1FilterRecord;
DAMemDataTable1.Filtered := True;
end;
Filtering by a range
When datatable is sorted using the field list, then application may apply filtering by a field values range. This is most effective way to limit records, as it is using the datatable internal index structures.
The following methods control the filtering:
- SetRangeStart - indicates that subsequent assignments to field values specify the start of the range of rows to include in the datatable;
- EditRangeStart - enables changing the starting value for an existing range;
- SetRangeEnd - indicates that subsequent assignments to field values specify the end of the range of rows to include in the datatable;
- EditRangeEnd - enables changing the ending value for an existing range;
- ApplyRange - applies a range to the datatable after setting of starting and ending values;
- SetRange - sets the starting and ending values of a range and applies them;
- CancelRange - removes any ranges currently in effect for the datatable;
DAMemDataTable1.SetRangeStart;
DAMemDataTable1.FieldByName('OrderID').AsInteger := 1001;
DAMemDataTable1.SetRangeEnd;
DAMemDataTable1.FieldByName('OrderID').AsInteger := 1051;
DAMemDataTable1.ApplyRange;
..
DAMemDataTable1.CancelRange;
is equal to
DAMemDataTable1.IndexFieldNames := 'OrderID';
DAMemDataTable1.SetRange([1001], [1051]);
..
DAMemDataTable1.CancelRange;