Recent

Author Topic: Create something right clicking DBGrid column titles  (Read 2515 times)

Heinz

  • New member
  • *
  • Posts: 9
Create something right clicking DBGrid column titles
« on: October 17, 2021, 04:00:30 pm »
Hi everyone,

I want to create a different Popup for each column. Since the columns order can be changed I need to identify that by the column title but I have not found a solution.
What I'm looking to do is to create an IF like this that handle on right clicking column title:

if selected column name = 'test' then begin showmessage('You have selected test column'); end;

I'll use the if to create dynamic popup to apply filters.
« Last Edit: October 18, 2021, 09:58:28 pm by Heinz »

Heinz

  • New member
  • *
  • Posts: 9
Re: TDBGrid and filters
« Reply #1 on: October 18, 2021, 05:20:59 pm »
I am thinking to right click on the title of a normal DBGrid to show a PopUp and select predefined filters.
I have tried selecting Columns with GetMem code:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  2.     Shift: TShiftState; X, Y: Integer);
  3. var
  4.   ACol, ARow: Integer;
  5. begin
  6.   if Y < DBGrid1.DefaultRowHeight then
  7.   begin
  8.     (Sender as TDBGrid).MouseToCell(X, Y, ACol, ARow);
  9.     if Button = mbRight then
  10.     begin
  11.     if DBGrid1.SelectedColumn.FieldName = 'Title1' then
  12.       BEGIN
  13.         ShowMessage('Title1'+ IntToStr(ACol));
  14.       end;
  15.  
  16.       if DBGrid1.SelectedColumn.FieldName = 'Title2' then
  17.       BEGIN
  18.         ShowMessage('Title2'+ IntToStr(ACol));
  19.       end;    
  20. end;
  21. end;
  22. end;
But it is not functional because it identify the columns by id and not the name so if the user move the columns position it will not work fine.
The result for each column is "Title1" + column positiong id.

« Last Edit: October 18, 2021, 05:51:18 pm by Heinz »

Hansvb

  • Sr. Member
  • ****
  • Posts: 399
Re: TDBGrid and filters
« Reply #2 on: October 18, 2021, 07:11:12 pm »
Hi, You can make a border-free window and compose your own filters.
In the header mouseclick, I get the column index and from that the header text. So it does not matter how you arrange you're columns. Or even have different columns every time.
The print screen is not made with Pascal but I must have somewhere some Pascal code that makes such a screen and build the filter.

OS Windows 10 - Lazarus 2.0.12

Heinz

  • New member
  • *
  • Posts: 9
Re: TDBGrid and filters
« Reply #3 on: October 18, 2021, 07:28:28 pm »
That's an advanced filter. It would be good but predefined controls applied by a PopUp are enough for me.
I don't know how to build an if for column names.

IF selected column name = 'test' then

I have found this:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.Button2Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   CaptionText: string;
  5. begin
  6.   for i := 0 to DBGrid1.Columns.Count - 1 do
  7.   case DBGrid1.Columns[i].FieldName of
  8.     'TEST':
  9.       begin
  10.         DBGrid1.Columns[i].Title.Caption := 'REPLACE TEXT';
  11.       end;
  12.   end;  
  13. end;    

It's not working though
, it replace text to every columns so is not selecting a particular column selected.
« Last Edit: October 18, 2021, 09:56:54 pm by Heinz »

devEric69

  • Hero Member
  • *****
  • Posts: 644
Re: Create something right clicking DBGrid column titles
« Reply #4 on: October 18, 2021, 10:21:17 pm »
[probably off-topic on]
I don't think that I'm really understanding what you're trying to do (generally, when we click on a header-title, it's to change the SQL 'ORDER BY' underneath, or change the dataset.IndexName used). Anyway, without "header's popup solution" (sorry), if you want to "filter" according the columns-fields displayed in your grid, you could put a TPanel on the top of the grid. Then:
- dynamically create some TLabeledEdit, having for TLabel.caption the titles of your columns-fields of the grid.
- on each TLabeledEdit.onChange event, you could modify the query of your grid:
Code: Pascal  [Select][+][-]
  1. with grdFoo.datasource.dataset do begin
  2.     Close;
  3.     SQL.Clear;
  4.     SQL:= 'SELECT ' + edtField1.label.caption + ', ' + edtField2.label.caption + ', ' + ...\... + ' FROM tbl_foo';
  5.     if (edtField1.text<>'') or (edtField2.text<>'') or  ...\... then begin
  6.         SQL.Add('WHERE');
  7.         if (edtField1.text<>'') then
  8.             SQL.Add(edtField1.label.caption + ' LIKE "%' + edtField1.text + '%"');
  9.         if (edtField2.text<>'') then
  10.             SQL.Add('AND ' + edtField2.label.caption + ' LIKE "%' + edtField2.text + '%"');
  11.         ...\...
  12.     end;
  13.     Open;
  14. end;
[probably off-topic off]
« Last Edit: October 18, 2021, 10:37:35 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

jesusr

  • Sr. Member
  • ****
  • Posts: 481
Re: TDBGrid and filters
« Reply #5 on: October 18, 2021, 11:19:19 pm »
I am thinking to right click on the title of a normal DBGrid to show a PopUp and select predefined filters.
I have tried selecting Columns with GetMem code:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  2.     Shift: TShiftState; X, Y: Integer);
  3. var
  4.   ACol, ARow: Integer;
  5. begin
  6.   if Y < DBGrid1.DefaultRowHeight then
  7.   begin
  8.     (Sender as TDBGrid).MouseToCell(X, Y, ACol, ARow);
  9.     if Button = mbRight then
  10.     begin
  11.     if DBGrid1.SelectedColumn.FieldName = 'Title1' then
  12.       BEGIN
  13.         ShowMessage('Title1'+ IntToStr(ACol));
  14.       end;
  15.  
  16.       if DBGrid1.SelectedColumn.FieldName = 'Title2' then
  17.       BEGIN
  18.         ShowMessage('Title2'+ IntToStr(ACol));
  19.       end;    
  20. end;
  21. end;
  22. end;
But it is not functional because it identify the columns by id and not the name so if the user move the columns position it will not work fine.
The result for each column is "Title1" + column positiong id.
In the DbGrid OnMouseDown hancler you can call zone := grid.MouseToRecordOffset(x, y, column, offset); if the returned column is not nil you can use it to show the right popup menu. If you want to be sure you clicked a column header check the zone return value.

Heinz

  • New member
  • *
  • Posts: 9
Re: Create something right clicking DBGrid column titles
« Reply #6 on: October 19, 2021, 05:19:44 pm »
Thank you

 

TinyPortal © 2005-2018