Dataset Filtering
TTMSFNCDataSetFilterDialog builds filter expressions for a Delphi dataset. Assign DataSet before showing the dialog so the available fields can be discovered and the generated FilterText can be applied.
Show the Dialog
Assign an active dataset to DataSet, then call Execute. When the user applies the dialog, the component updates the dataset filter text and toggles filtering when needed.
TMSFNCDataSetFilterDialog1.DataSet := ClientDataSet1;
TMSFNCDataSetFilterDialog1.FilterText := ClientDataSet1.Filter;
TMSFNCDataSetFilterDialog1.Execute;
Seed or Read Filter Text
Use FilterText when the dialog should open with an existing filter or when the application needs to inspect the generated expression after execution.
TMSFNCDataSetFilterDialog1.DataSet := ClientDataSet1;
TMSFNCDataSetFilterDialog1.FilterText := 'Status = ''Open''';
TMSFNCDataSetFilterDialog1.Execute;
Caption := TMSFNCDataSetFilterDialog1.FilterText;
Customize Dialog Events
Handle button events when the application needs to control whether the dialog closes, clear the filter in a custom way, or log generated filter expressions. Handle OnGetFormatSettings when date, time, or number formatting must match a specific locale.
procedure TForm1.TMSFNCDataSetFilterDialog1ApplyButtonClicked(Sender: TObject;
AFilterBuilder: TTMSFNCFilterBuilder; var AClose: Boolean);
begin
Caption := AFilterBuilder.FilterText;
AClose := True;
end;
procedure TForm1.TMSFNCDataSetFilterDialog1GetFormatSettings(Sender: TObject;
var AFormatSettings: TFormatSettings);
begin
AFormatSettings.ShortDateFormat := 'yyyy-mm-dd';
end;
Combining dialog execution, seeded filter text, and result handling
The following example seeds the dialog with a previously saved filter expression, applies the result to the dataset, and logs the generated text:
procedure TForm1.btnFilterClick(Sender: TObject);
begin
TMSFNCDataSetFilterDialog1.DataSet := ClientDataSet1;
TMSFNCDataSetFilterDialog1.FilterText := FLastFilter; // restore previous
if TMSFNCDataSetFilterDialog1.Execute then
begin
FLastFilter := TMSFNCDataSetFilterDialog1.FilterText;
ClientDataSet1.Filter := FLastFilter;
ClientDataSet1.Filtered := FLastFilter <> '';
Memo1.Lines.Add('Filter applied: ' + FLastFilter);
end;
end;
procedure TForm1.TMSFNCDataSetFilterDialog1ApplyButtonClick(Sender: TObject;
var ACanClose: Boolean);
begin
// Prevent closing if the filter text is empty
ACanClose := TMSFNCDataSetFilterDialog1.FilterText <> '';
if not ACanClose then
ShowMessage('Please define at least one filter condition.');
end;