Recent

Author Topic: Set the DBGrid column width for a SQLite3 table  (Read 6084 times)

banffguy

  • New Member
  • *
  • Posts: 11
Set the DBGrid column width for a SQLite3 table
« on: October 05, 2016, 12:19:32 am »
Lazarus 1.6,FPC 3.0,sqlite3.8.3.1
Have table works fine in grid and other functions work OK
Want to set the column widths for each colum,
A statement; Form1.DBGrid1.columns[4].width := 112; has no effect on the
column width.  Works fine using a Tdbf table but not for SQLite.
Anyone have an answer or a solution?
Or is the only to change the columne through the sqlite dot commands
Thanks

totya

  • Hero Member
  • *****
  • Posts: 722
Re: Set the DBGrid column width for a SQLite3 table
« Reply #1 on: October 05, 2016, 02:51:29 am »
Hi!

It's working perfectly for me, with TSQLite3Dataset.

LacaK

  • Hero Member
  • *****
  • Posts: 696
Re: Set the DBGrid column width for a SQLite3 table
« Reply #2 on: October 05, 2016, 07:31:35 am »
How do you define columns in your DBGrid1 ? At design time or at runtime ?
I guess, that you have "empty" grid at design time and columns are created at runtime dynamicaly based on underlaying DataSource.
Then you must set column Width after each opening of DataSource ...

banffguy

  • New Member
  • *
  • Posts: 11
(Solved) Set the DBGrid column width for a SQLite3 table
« Reply #3 on: October 06, 2016, 06:43:52 am »
Did the column settings in the Object Inspectorand it works fine, was trying to
make changes by code but didn't seem to work.  Not a big problem thought it might be easier debugging and adjusting when laying out the grid.
Thanks for your comments and help.
Banffguy

totya

  • Hero Member
  • *****
  • Posts: 722
Re: Set the DBGrid column width for a SQLite3 table
« Reply #4 on: October 06, 2016, 06:48:05 am »
LacaK wrote:

Quote
Then you must set column Width after each opening of DataSource ...

Plaese show your code, or create simple demo app of this problem, and attach here.

vrull

  • Full Member
  • ***
  • Posts: 118
Re: Set the DBGrid column width for a SQLite3 table
« Reply #5 on: November 08, 2016, 11:43:46 pm »
The below procedure adjusts columns:
Code: Pascal  [Select][+][-]
  1. procedure TfrmGrid.SmartColumnAdjust;
  2. var
  3.   i : integer;
  4.   intSize, colSize : integer;
  5. begin
  6.   intSize := ReadSetting('fixedColumnSize', 80); // this sets the minimal width of non-adjustable columns, 80 is the default width
  7.   for i := 0 to Pred(DBGrid.Columns.Count) do
  8.     if not (DBGrid.Columns[i].Field.DataType in [ftString, ftMemo, ftWideString, ftWideMemo, ftFmtMemo]) then // all columns with text data are auto-adjusted, so we skip them
  9.       begin // this is non-adjustable column
  10.         DBGrid.Columns[i].SizePriority := 0; // this prevents auto-adjust by internal routine
  11.         colSize := IntMax(intSize, DBGrid.Canvas.GetTextWidth(DBGrid.Columns[i].Title.Caption)); // max of column title width or default width
  12.         DBGrid.Columns[i].Width := colSize; // set the width
  13.       end;
  14.   DBGrid.AutoSizeColumns; // force setting auto-adjustable columns
  15. end;
  16.  

 

TinyPortal © 2005-2018