autofit dbgrid column size through code? lazarus / 16-Databases ---------------------------------------------------------------------- tatamata · 2007-09-23 18:24 Hello. How to program auto-fit behavior of column size in dbgrid? Thanks, Zlatko ---------------------------------------------------------------------- tatamata · 2007-09-24 06:50 Is there any alternative grid component for Lazarus, beside the standard DBGrid? ---------------------------------------------------------------------- alexs · 2007-09-24 12:19 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 · 2007-09-25 11:53 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 · 2007-09-25 12:28 it's compile with last version 0.9.23 from svn ---------------------------------------------------------------------- tatamata · 2007-09-25 17:30 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 · 2007-09-26 18:16 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 · 2007-09-27 05:04 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 · 2007-09-27 09:46 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 · 2007-09-27 18:38 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 · 2007-09-28 05:18 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 · 2007-09-28 10:55 Thanks, Alex, that's it! "Caption" needs to be added after "Title": RxDBGrid.Canvas.TextWidth(Columns.Title.Caption)...and works great. Regards, ZLatko