Recent

Author Topic: [SOLVED] TDBGrid cell widths  (Read 908 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: 11855
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.

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: 949
  • ..... A day not Laughed is a day wasted !!
    • 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 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018