Recent

Author Topic: [SOLVED] TDBGrid cell widths  (Read 304 times)

guest48180

  • Guest
[SOLVED] TDBGrid cell widths
« on: August 11, 2019, 04:55:26 pm »
I use several DBGrids in this one program. Every user will undoubtedly want to grab the cell headers with the mouse and slide them around to adjust the cell widths to what they prefer. Is there a way to capture these widths and write them to a db so they they get loaded each time?

Also: and this may be a bug. Dunno. But i have a field in my sqlite3 db of type CHAR. When i launch my program, TDBGrid gives me a default width of about 2000. I changed that to VARCHAR(1) to fix it.


wp

  • Hero Member
  • *****
  • Posts: 6364
Re: TDBGrid cell widths
« Reply #1 on: August 11, 2019, 07:22:50 pm »
You need persistent columns for the dbgrid. Either create persistent fields at designtime, or create columns after the dataset has been loaded. Then you have a property DBGrid1.Columns[index].Width which you can store in an ini or xml config file.

The reason why some columns are ultra-wide is in the field definition. When you declare a string field without size restriction the grid prepares for a very long string. Again, when you create columns at designtime or at runtime after loading the db you can immediately apply a more meaningful value.

See attached demo.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

guest48180

  • Guest
Re: TDBGrid cell widths
« Reply #2 on: August 11, 2019, 08:35:49 pm »
That's clean, wp. Extremely helpful information. Thanks for passing it on.

madref

  • Hero Member
  • *****
  • Posts: 696
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
Re: [SOLVED] TDBGrid cell widths
« Reply #3 on: August 24, 2019, 06:58:07 pm »
Just an addition:
You can also do this:
Code: Pascal  [Select]
  1. type
  2.   TMyDBGridHelper = class Helper for TDBGrid
  3.   public
  4.     function _ColumnByName(sFieldname: string): TColumn;
  5.   end;  // TMyDBGridHelper
  6.  
and then then this is possible:
Code: Pascal  [Select]
  1.   DBGrid_Observaties_Zwakte._ColumnByName('Competence').Width := 430;
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.2 / FPC 3.0.4
Lazarus Trunc / FPC 3.0.4
Mac OS X Mojave