I have client table and i want to filter the table on the field chosen by the user all table fields included in combobox component at run time if the field/item is string type whatever the user type in EdtSearch.Text the filter work however if the user chose the id which is firebird integer auto_inc field get exception:

Exception class EVariantTypeCastError with message 'Could not convert variant of type (OleStr) into type (Boolean)'.

Code:

procedure Tfrm_Personnes.EdtSearchChange(Sender: TObject);
var
  Pattern: string;
begin
  if CbxSearchOptions.Text = 'Commence' then Pattern := QuotedStr(EdtSearch.Text +     '*');
  if CbxSearchOptions.Text = 'Contient' then Pattern := QuotedStr('*' + EdtSearch.Text + '*');
  with TClientDataSet(dts_Tableau_Personnes.DataSet) do
  begin
    if EdtSearch.Text <> EmptyStr then
    begin
      Filter := DisplayToOriginName(dts_Tableau_Personnes, CbxField.Text)+' = ' +     Pattern;
      Filtered := True;
    end else
      Filtered := False;
  end;
end;

function DisplayToOriginName(DataSource: TDataSource; DisplayName: string): string;
var
  I: Integer;
begin
  with TClientDataSet(DataSource.DataSet) do
  for I := 0 to FieldCount - 1 do
  begin
    if SameStr(Fields[i].DisplayName, DisplayName) then
      Result  := Fields[i].FieldName;
  end;
end;

what is the proper way to get the filter work on integer field thank's

有帮助吗?

解决方案

the best solution is to use OnFilterRecord Event from the documentation:

OnFilterRecord events generated by the dataset for each record it retrieves

the event has parameter by reference Accept which determine whether the record is accepted (Example: include it in the DBGride) or not however you can't use wildcard character and other filter method features

If the ClientDataSet already filtered then you must change the property to False then True in order to make the filter work properly.

procedure Tfrm_Personnes.EdtSearchChange(Sender: TObject);
begin
  MyClientDataSet.Filtered := False;
  MyClientDataSet.Filtered := True;
end;

procedure TDM_Tableau.cds_ClientsFilterRecord(DataSet: TDataSet;
  var Accept: Boolean);
var
  s, SubStr: string;
begin
  s := DataSet.FieldByName('ID_ClIENT').AsString;
  SubStr := frm_Clients.EdtSearch.Text;
  Accept := Pos(SubStr ,s) > 0;
end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top