* * *

Author Topic: [SOLVED] How to filtering a grid with an editbox  (Read 612 times)

dquinter

  • New member
  • *
  • Posts: 7
[SOLVED] How to filtering a grid with an editbox
« on: August 03, 2017, 09:05:13 pm »
Hello everyone,
I’m new to Lazarus (And Pascal) and i think i have an easy problem, but since I’m new, I have no idea how to fix it.

I’m trying to make an application for a database made in Firebird, so I have a TZConnection (I'm using Zeos), my TZQuery, which is a select * from table, and my TDataSource linked to my TZQuery.

My problem is filtering of a grid with the help of an editbox.
I want to filter the grid, with what I type into the editbox, so when I type in someting like "LAZ", the grid only shows the records with "LAZ" in the field I’m searching in, foe example, a record with LAZARUS in the text.

So I’m thinking of an onchange event for the editbox, but after that I’m lost.

Please help.  :) :) :)
« Last Edit: August 11, 2017, 09:00:15 pm by dquinter »

mangakissa

  • Hero Member
  • *****
  • Posts: 710
Re: How to filtering a grid with an editbox
« Reply #1 on: August 04, 2017, 08:35:11 am »
sourcecode could be handly.

How do you filter your dbgrid (actually dataset) in your code.
Do you recall the query with a where clause or with the property filter?
Lazarus 1.64 (32b) / FPC 3.0
Windows 10

GetMem

  • Hero Member
  • *****
  • Posts: 2253
Re: How to filtering a grid with an editbox
« Reply #2 on: August 04, 2017, 10:11:16 am »
@dquinter
Quote
Hello everyone,
I’m new to Lazarus (And Pascal) and i think i have an easy problem, but since I’m new, I have no idea how to fix it.

I’m trying to make an application for a database made in Firebird, so I have a TZConnection (I'm using Zeos), my TZQuery, which is a select * from table, and my TDataSource linked to my TZQuery.

My problem is filtering of a grid with the help of an editbox.
I want to filter the grid, with what I type into the editbox, so when I type in someting like "LAZ", the grid only shows the records with "LAZ" in the field I’m searching in, foe example, a record with LAZARUS in the text.

So I’m thinking of an onchange event for the editbox, but after that I’m lost.

1. Create OnChange event for the Edit
2. Create OnFilterRecord event for the Query
3. Don't forget to change "YOURFIELD"
Code: Pascal  [Select]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   if Trim(Edit1.Text) <> '' then
  4.   begin
  5.     if not SQLQuery1.Filtered then
  6.       SQLQuery1.Filtered := True;
  7.     SQLQuery1.Refresh;
  8.   end
  9.   else
  10.     SQLQuery1.Filtered := False;
  11. end;
  12.  
  13. procedure TForm1.SQLQuery1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
  14. begin
  15.   Accept := Pos(UpperCase(Edit1.Text), UpperCase(DataSet.FieldByName('YOURFIELD').AsString)) > 0;
  16. end;
« Last Edit: August 04, 2017, 12:12:24 pm by GetMem »

fiazhnd

  • New member
  • *
  • Posts: 27
Re: How to filtering a grid with an editbox
« Reply #3 on: August 05, 2017, 04:59:59 pm »
Code: Pascal  [Select]
  1. procedure TForm1.EditSearchChange(Sender: TObject);
  2. begin
  3.  if EditSearch.Text='' then SQLQuery1.Filtered:=false else
  4.  begin
  5.    SQLQuery1.Filtered:=false;
  6.     SQLQuery1.Filter:=' Name LIKE '+ QuotedStr('*'+EditSearch.Text+'*') +
  7.                      'OR CName LIKE '+ QuotedStr('*'+EditSearch.Text+'*') +
  8.                      'OR City LIKE '+ QuotedStr('*'+EditSearch.Text+'*')+
  9.                      'OR Phone LIKE '+ QuotedStr('*'+EditSearch.Text+'*')+
  10.                      'OR Car LIKE '+ QuotedStr('*'+EditSearch.Text+'*');
  11.   SQLQuery1.Filtered:=true;
  12.  end;
  13.  end;
  14.  
« Last Edit: August 05, 2017, 05:03:09 pm by fiazhnd »

dquinter

  • New member
  • *
  • Posts: 7
Re: How to filtering a grid with an editbox
« Reply #4 on: August 11, 2017, 08:56:52 pm »
sourcecode could be handly.

How do you filter your dbgrid (actually dataset) in your code.
Do you recall the query with a where clause or with the property filter?

I did someting like this:

Code: Pascal  [Select]
  1.   procedure TfrmClientes.btnBuscarClick(Sender: TObject);
  2.   begin
  3.     DClientes.Close;
  4.     DClientes.SQL.Clear;
  5.     DClientes.SQL.Add('select * from CLIENTES');
  6.     DClientes.SQL.Add('where upper(NOME) like :BUSCAR');
  7.     DClientes.SQL.Add('order by NOME');
  8.     DClientes.ParamByName('BUSCAR').AsString := UpperCase(Format('%s%s%s',['%', Trim(edtBuscar.Text),'%']));
  9.     DClientes.Prepare;
  10.     DClientes.Open;
  11.   end;
  12.  

But I'll try the other methods than GetMem and fiazhnd told me.

Thanks to all! :)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus