Recent

Author Topic: Hiding a column at runtime  (Read 4644 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Hiding a column at runtime
« on: June 30, 2015, 01:59:55 pm »
Hi,

I am totally new to LazReport, and haven't written reports in 10 years. I have a simple report with 5 columns of data. I have a print dialog (before the report appears) where the user can make some selections. If a specific selection is made (checkbox), I need to hide a column in the report (header & data field). How do I accomplish this with LazReport?

Any help would be very much appreciated.

Regards,
  Graeme
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Hiding a column at runtime
« Reply #1 on: June 30, 2015, 04:01:37 pm »
Cover the offending part with a paper.
Specialize a type, not a var.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Hiding a column at runtime
« Reply #2 on: June 30, 2015, 04:13:53 pm »
Yeah, nice tip.  %)
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Hiding a column at runtime
« Reply #3 on: June 30, 2015, 04:57:08 pm »
I've used File -> Variable List to define a new variable (just the name) and used Type -> Other -> Expression. I set the default value to 1.

I then double clicked on a header memo field and enabled the Script
checkbox, and typed in the following code in the Script box:

Code: [Select]
if [HideKlas] = 1 then
  Visible := False;

If I preview the report now it works as expected, the column is hidden. Changing the variable's hard-coded value to 0, and the column becomes visible in the preview. So far so good.

Now to try and set the variable from my application code. In my *.pas unit I temporarily tried to set a hard-coded value of the
"HideKlas" variable to see what happens in the report. I tried both the
values '0' and '1'.

Code: [Select]
If FRPrint.PrepareReport then
begin
  FRPrint.Variables.Values['HideKlas'] := '1';  // for testing only
  FRPrint.Preview := FAfdrukForm.PrintPanel;
  FRPrint.ShowPreparedReport;
end;

No matter what I do, the header (memo) field is not affected. It seems to always use the hard-coded value as I defined it when I created the variable in the report designer.

So I made a little progress, but still stuck. :-(
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
[Solved] Re: Hiding a column at runtime
« Reply #4 on: June 30, 2015, 05:31:39 pm »
Digging deeper into the LazReport code, I finally found the solution. G*d, but LazReport's API is so inconsistent and confusing!!!    >:(

Even though you setup a variable, you must not refer to it via the Report.Script.Values[] or Report.Variables.Values[] properties. Go figure!

Instead you use the Report.Values.FindVariables() method.

Code: [Select]
Report.Values.FindVariable('HideKlas').Field := '1';

With this change, and the code from my previous message, I finally have a working report where I can show/hide a column of data at runtime.
« Last Edit: July 03, 2015, 07:31:58 am by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

 

TinyPortal © 2005-2018