RAD Studio |  |

Writing an OnFilterRecord Event Handler
See Also
Collapse All

You can write code to filter records using the 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":

Copy Code

    procedure TForm1.Table1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
    begin
      Accept := DataSet['State'].AsString = 'CA';
    end;

Copy Code

    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 runtime. For example, the following statements switch to an OnFilterRecord event handler called NewYorkFilter:

Copy Code

    DataSet1.OnFilterRecord := NewYorkFilter;
    Refresh;

Copy Code

    DataSet1->OnFilterRecord = NewYorkFilter;
    ()

See Also

Setting the Filter Property

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!