Recent

Author Topic: autofit dbgrid column size through code?  (Read 13859 times)

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« on: September 23, 2007, 08:24:48 pm »
Hello.
How to program auto-fit behavior of column size in dbgrid?
Thanks,

Zlatko

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #1 on: September 24, 2007, 08:50:57 am »
Is there any alternative grid component for Lazarus, beside the standard DBGrid?

alexs

  • New Member
  • *
  • Posts: 19
    • http://alexs75.narod.ru/
autofit dbgrid column size through code?
« Reply #2 on: September 24, 2007, 02:19:00 pm »
see RxDBGrid from rxfpc - in lazarus-ccr or on url http://alexs75.narod.ru/fpc/rxfpc/RxFPC.rar
in RxDBGrid auto-fit collumn present ...

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #3 on: September 25, 2007, 01:53:07 pm »
Alexs,
1. I couldn't install rxfpc in Lazarus 0.9.22, some methods can't be overriden (not present in parent class?)...Which version works with Lazarus 0.9.22?
2. Why AutoAdjustColumns method doesn't give expected result for DBGrid?

Regards,

Zlatko

alexs

  • New Member
  • *
  • Posts: 19
    • http://alexs75.narod.ru/
autofit dbgrid column size through code?
« Reply #4 on: September 25, 2007, 02:28:55 pm »
it's compile with last version 0.9.23 from svn

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #5 on: September 25, 2007, 07:30:26 pm »
OK. I installed 0.9.23, fpc 2.2.1 and installed RxFpc.
But I still don't know how to set column size to be automatically adjust...

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #6 on: September 26, 2007, 08:16:10 pm »
Hello again.
I figured out that I can use RxDBGrid's OptimizeColumnsWidthAll method to automatically adjust columns width proportionally to its content.
But, I experienced strange behavior of that method with persistent columns based on Firebird ZQuery dataset. Columns are adjusted to the database field size, not to actual field's content. For example, if a field is VARCHAR(255), the corresponding  RxDBGrid column will be 255 characters wide, although actual content of cells are much shorter than 255 characters !?
If I use dynamically created columns, autoadjustment works correctly.

Just to mention, CheckBox columns in DBGrid don't function correctly in current Lazarus 0.9.23 fpc 2.2.1 snapshot. There's no checkbox in a grid, only blank uneditable cell...I hope Lazarus developers already know that...

Regards,

Zlatko

alexs

  • New Member
  • *
  • Posts: 19
    • http://alexs75.narod.ru/
autofit dbgrid column size through code?
« Reply #7 on: September 27, 2007, 07:04:06 am »
Quote

But, I experienced strange behavior of that method with persistent columns based on Firebird ZQuery dataset. Columns are adjusted to the database field size, not to actual field's content


i'm use FireBird and FBDataSet - RxDBGrid.OptimizeColumnsWidthAll and RxDBGrid1.OptimizeColumnsWidth('Field_Name') work ok

Quote

Just to mention, CheckBox columns in DBGrid don't function correctly in current Lazarus 0.9.23

set TitleStyle of DBGrid to tsStandard

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #8 on: September 27, 2007, 11:46:46 am »
No, changing TitleStyle didn't help. cbsCheckBoxColumn just doesn't work (at least for Integer fields, I don't know for Booleans, as Firebird doesn't have Boolean data type)...
I think it is a bug, because in Lazarus 0.9.22 fpc 2.0.4 this works OK.

Regards,

Zlatko

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #9 on: September 27, 2007, 08:38:19 pm »
OptimizeColumnsWidthAll optimizes column width according to data width in cells, but how to adjust column width according to Title width, in cases when title width is longer than text in cells?
Regards,
Zlatko

alexs

  • New Member
  • *
  • Posts: 19
    • http://alexs75.narod.ru/
autofit dbgrid column size through code?
« Reply #10 on: September 28, 2007, 07:18:13 am »
Code: [Select]

RxDBGrid1.OptimizeColumnsWidthAll;
for i:=0 to RxDBGrid1.Columns.Count-1 do
  if  RxDBGrid1.Canvas.TextWidth(RxDBGrid1.Columns[i].Title)>RxDBGrid1.Columns[i].Width then
  RxDBGrid1.Columns[i].Width :=RxDBGrid1.Canvas.TextWidth(RxDBGrid1.Columns[i].Title);

tatamata

  • Hero Member
  • *****
  • Posts: 749
    • ZMSQL - SQL enhanced in-memory database
autofit dbgrid column size through code?
« Reply #11 on: September 28, 2007, 12:55:00 pm »
Thanks, Alex, that's it!
"Caption" needs to be added after "Title":
RxDBGrid.Canvas.TextWidth(Columns.Title.Caption)...and works great.
Regards,

ZLatko

 

TinyPortal © 2005-2018