Writing an OnFilterRecord Event Handler

From RAD Studio
Jump to: navigation, search

Go Up to Creating Filters


You can write code to filter records using the Data.DB.TDataSet.OnFilterRecord events generated by the dataset for each record it retrieves. This event handler implements a test that determines if a record should be included in those that are visible to the application.

To indicate whether a record passes the filter condition, your OnFilterRecord handler sets its Accept parameter to True to include a record, or False to exclude it. For example, the following filter displays only those records with the State field set to "CA":

procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept := DataSet['State'].AsString = 'CA';
end;
void __fastcall TForm1::Table1FilterRecord(TDataSet *DataSet; bool &Accept)
{
  Accept = DataSet->FieldByName["State"]->AsString == "CA";
}

When filtering is enabled, an OnFilterRecord event is generated for each record retrieved. The event handler tests each record, and only those that meet the filter's conditions are displayed. Because the OnFilterRecord event is generated for every record in a dataset, you should keep the event handler as tightly coded as possible to avoid adversely affecting the performance.

You can code any number of OnFilterRecord event handlers and switch among them at run time. For example, the following statements switch to an OnFilterRecord event handler called NewYorkFilter:

DataSet1.OnFilterRecord := NewYorkFilter;
Refresh;
DataSet1->OnFilterRecord = NewYorkFilter;
()

See Also