Forum > Databases

Create something right clicking DBGrid column titles

(1/2) > >>

Heinz:
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.

Heinz:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm2.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;    Shift: TShiftState; X, Y: Integer);var  ACol, ARow: Integer;begin  if Y < DBGrid1.DefaultRowHeight then  begin    (Sender as TDBGrid).MouseToCell(X, Y, ACol, ARow);    if Button = mbRight then    begin    if DBGrid1.SelectedColumn.FieldName = 'Title1' then      BEGIN        ShowMessage('Title1'+ IntToStr(ACol));      end;       if DBGrid1.SelectedColumn.FieldName = 'Title2' then      BEGIN        ShowMessage('Title2'+ IntToStr(ACol));      end;     end;end;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.

Hansvb:
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.

Heinz:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm2.Button2Click(Sender: TObject);var  i: Integer;  CaptionText: string;begin  for i := 0 to DBGrid1.Columns.Count - 1 do  case DBGrid1.Columns[i].FieldName of    'TEST':      begin        DBGrid1.Columns[i].Title.Caption := 'REPLACE TEXT';      end;  end;  end;    
It's not working though, it replace text to every columns so is not selecting a particular column selected.

devEric69:
[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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---with grdFoo.datasource.dataset do begin    Close;    SQL.Clear;    SQL:= 'SELECT ' + edtField1.label.caption + ', ' + edtField2.label.caption + ', ' + ...\... + ' FROM tbl_foo';    if (edtField1.text<>'') or (edtField2.text<>'') or  ...\... then begin        SQL.Add('WHERE');         if (edtField1.text<>'') then             SQL.Add(edtField1.label.caption + ' LIKE "%' + edtField1.text + '%"');        if (edtField2.text<>'') then             SQL.Add('AND ' + edtField2.label.caption + ' LIKE "%' + edtField2.text + '%"');        ...\...    end;    Open;end;[probably off-topic off]

Navigation

[0] Message Index

[#] Next page

Go to full version