Recent

Author Topic: "autosize" of IBDinamicGrid - header  (Read 505 times)

Nicole

  • Hero Member
  • *****
  • Posts: 631
"autosize" of IBDinamicGrid - header
« on: November 29, 2022, 08:44:27 pm »
How can I get the width of the cell to fit the header?
see screenshot

dsiders

  • Hero Member
  • *****
  • Posts: 761
Re: "autosize" of IBDinamicGrid - header
« Reply #1 on: November 29, 2022, 09:35:30 pm »
How can I get the width of the cell to fit the header?
see screenshot

Did you set a Width for those column definitions in the Columns property? It is used as a minimum width, even when AutoSizeColumn is enabled.
Preview Lazarus 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

KodeZwerg

  • Hero Member
  • *****
  • Posts: 941
  • Fifty shades of code.
    • Delphi & FreePascal
Re: "autosize" of IBDinamicGrid - header
« Reply #2 on: November 29, 2022, 10:23:51 pm »
How can I get the width of the cell to fit the header?
see screenshot
You can call this method to calculate needed space for your elements but you need to rethink about your doing by adding more checks since on some of your columns the cell content is larger than your header text is long.

Example (not using your control but it works same way with any control)
Code: Pascal  [Select][+][-]
  1. uses .... LCLIntf, LCLType;
  2.  
  3. // calculate exact size of needed space for a control on screen
  4. function TextToRect(const AText: String; const AFont: TFont): TRect;
  5. var
  6.   bmp: TBitmap;
  7.   R: TRect;
  8. begin
  9.   bmp := TBitmap.Create;
  10.   try
  11.     bmp.SetSize(1, 1);
  12.     bmp.Canvas.Font.Assign(AFont);
  13.     R := Rect(0, 0, 0, 0);
  14.     DrawText(bmp.Canvas.Handle, PChar(AText), Length(AText), R, DT_CALCRECT or DT_SINGLELINE);
  15.     Result := R;
  16.   finally
  17.     bmp.Free;
  18.   end;
  19. end;
  20.  
  21. { TForm1 }
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. var
  25.   r: TRect;
  26. begin
  27.   r := TextToRect(Panel1.Caption, Panel1.Font);
  28.   // r has now the exact size, so you should add some space on your own, like for border...
  29.   Panel1.Width := r.Width;
  30.   Panel1.Height := r.Height;
  31. end;

On picture you see what is happening
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 631
Re: "autosize" of IBDinamicGrid - header
« Reply #3 on: November 30, 2022, 06:53:12 pm »
Thank you for the answers.

I will try it without owner-draw if I can avoid it, maybe there is something basic wrong.
This columns property was set to zero and the fields empty (I have an IBQuery with fills the DynamicGrid at runtime).

After I tried to add something at design time and clicked "add fields" (I am not sure and do not want to try it again), the Lazarus IDE set goodbye to me with thunder and fog.

In Delphi there was a field for DBGrid, which allowed me to choose "all query fields". Such a function I have not found in Lazarus / DynamciGrid. So may be, there is something very substantial missing in my code.

Please forgive me, if I mix up menues, queris and data-components.
This looks all similar to me.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 941
  • Fifty shades of code.
    • Delphi & FreePascal
Re: "autosize" of IBDinamicGrid - header
« Reply #4 on: November 30, 2022, 07:02:27 pm »
Maybe you misunderstood my method, it has nothing to do with OwnerDrawn, just iterate over your columns headers, take caption and set calculated width for it.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Nicole

  • Hero Member
  • *****
  • Posts: 631
Re: "autosize" of IBDinamicGrid - header
« Reply #5 on: December 01, 2022, 03:30:34 pm »
@KodeZwerg, DOES IT?
Oh yes, I misunderstood it.

I will save it and keep it for the next step in my software on the ToDo list.
Thank you so much for it!!

If it works for me, then it is something, what I needed since a long time.
This has only worked half in Delphi as well.


tonyw

  • Sr. Member
  • ****
  • Posts: 294
    • MWA Software
Re: "autosize" of IBDinamicGrid - header
« Reply #6 on: December 02, 2022, 12:02:51 am »
How can I get the width of the cell to fit the header?
see screenshot
The objective of TIBDynamicGrid is to ensure that the coulmns always fit the grid even when it is dynamically resized at run time.

At design time you set the nominal width of each column. You also nominate one or more columns to be autosized. These columns are then dynamically resized to fit the current grid width and are resized in proportion to their original width.

Nicole

  • Hero Member
  • *****
  • Posts: 631
Re: "autosize" of IBDinamicGrid - header
« Reply #7 on: December 02, 2022, 01:18:00 pm »
The column widths are not-adjustd at runtime.

I fill them by "select * from tbanytable".
What does not adjust are the headers = the field names.

What may be the reason?
Can the overall width be too small for it?
Some table field names are not too short chosen years ago and not to change without huge effort.

If the DBGrid-width would be too small for all headers / field names, I rather would prefer the whole DBGrid to be scrolled.

tonyw

  • Sr. Member
  • ****
  • Posts: 294
    • MWA Software
Re: "autosize" of IBDinamicGrid - header
« Reply #8 on: December 02, 2022, 01:54:09 pm »
The column widths are not-adjustd at runtime.

I fill them by "select * from tbanytable".
What does not adjust are the headers = the field names.

What may be the reason?
Can the overall width be too small for it?
Some table field names are not too short chosen years ago and not to change without huge effort.

If the DBGrid-width would be too small for all headers / field names, I rather would prefer the whole DBGrid to be scrolled.
Are you using the IDE fields editor to add the field definitions to your form? You must do this if you are to specify the width of each column and which are dynamically sized. Look at the IBX employee example and see how the columns are set up in the example.

Nicole

  • Hero Member
  • *****
  • Posts: 631
Re: "autosize" of IBDinamicGrid - header
« Reply #9 on: December 02, 2022, 07:15:47 pm »
The employee example does not open for me, because the database does not open in my VM.

Can you pls give me a verbose hint to "the IDE fields editor"

There are a lot of field editors for columns. I tried. But did not find the correct one, of crashed on attempting to use it.

Maybe the whole feed of the Dynamic grid is not appropriate:
I have a datacource, a dataset and an IBUpdateSQL.
I am puzzled not on checking, can I have it done without query? I seemed have to have added my SQL to dataset.

and: Yes, it works as such, I can use the DBNavigator and change values in my db.


 

TinyPortal © 2005-2018