Update:
Delphi takes as default for Displaylength the length of the varchar field. varchar(255) = Displaylength = 255.
Can I change this behavior with "SQL". NO
If the result is only 99 characters long, the length varchar (255) = 255 and Displaylength = 255 .
Compare it!
allowed:
select left(name,99), quantity, value from table;
- Sql should quickly deliver a dataset (not the design of a report).
- For each table for each varchar field, you have to set an extra SQL statement
(xname,99)
- Can I see, truncated (perhaps important) information? Never.
wy not allowed:
Columns[index].Width = 99;
- Easier to design a report.
- Easier select SQL statement:
select name, quantity, value from table
- Simple expand column with mouse to see hidden information.
- Good Design: The column size of a DBGrid should be that you can see at least 2 columns at once.
The program should take over the design.
I can not do this by pascal code nor define it on the Fields object of the table component because this code comes from many tables as this is a report generator.
and for each report I have to use a different SQL code.
Who generates this "different SQL code"?
I think similar lines are used to generate the code (Pascal code).
Allowed: Pascal code
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add ('select name, quantity, value from table');
Query1.Active: = true;
why not allowed: Pascal code
Columns[index].Width := 99;
you can do it with an automatic adjustment of the columns. All columns wider a certain size are to be adapted to their actual width. That's still better than a fixed size = 99.
The following program has been tested with Rad Studio 2007 and Zeos Components.
Firebird 2.5, on a Windows 2000 computer.
- A) Zeos Components.
- B) DBGrid1 no active data connection.
- C) Firebird automatically allocated size for varchar (255).
- D) The running Program. "use Me" is used to automatically size the column.
For lack of time, I created only 2 data fields.
If the entire source code is required for download, let me know.
unit FBAuto;
{
~Zarko Gajic
About Delphi Programing
http://delphi.about.com
customized 2013
moskito-x software
}
interface
uses
Math,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, ExtCtrls, StdCtrls, ZAbstractRODataset,
ZAbstractDataset, ZDataset, ZConnection;
type
TAutoColumnWidth = record
Index : integer;
Width : integer;
end;
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ZConnection1: TZConnection;
ZQuery1: TZQuery;
DataSource1: TDataSource;
CBuseMe: TCheckBox;
DBGrid2: TDBGrid;
ZQuery2: TZQuery;
DataSource2: TDataSource;
IntegerField1: TIntegerField;
StringField1: TStringField;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure CBuseMeClick(Sender: TObject);
private
{ Private declarations }
procedure SetFit;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
AutoColumnWidth : TAutoColumnWidth;
useMe :Boolean;
procedure TForm1.SetFit;
var
i:integer;
begin
if NOT CBuseMe.Checked then begin
for i := 0 to DBGrid1.Columns.Count - 1 do begin
if DBGrid1.Columns[i].Field.Size > 300 then
DBGrid1.Columns[i].Width := DBGrid1.Columns[i].Field.Size;
end;
end else begin
useMe:=true;
for i := 0 to DBGrid1.Columns.Count - 1 do begin
AutoColumnWidth.Index:=-1;
if DBGrid1.Columns[i].Field.DisplayWidth > 200 then AutoColumnWidth.Index:=i;
if AutoColumnWidth.Index > -1 then begin
AutoColumnWidth.Width := -1;
DBGrid1.Repaint;
DBGrid1.Columns[AutoColumnWidth.Index].Width := AutoColumnWidth.Width+ 5;
end;
end;
useMe:=false;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
ZQuery1.Active:=true;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AutoColumnWidth.Index := -1;
AutoColumnWidth.Width := -1;
useMe:=false;
end;
procedure TForm1.CBuseMeClick(Sender: TObject);
begin
setFit;
end;
procedure TForm1.DBGrid1DrawColumnCell(
Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
begin
if useMe then begin
if DataCol = AutoColumnWidth.Index then
begin
if Assigned(Column.Field) then
begin
AutoColumnWidth.Width := Max(AutoColumnWidth.Width, DBGrid1.Canvas.TextWidth(Column.Field.DisplayText));
end;
end;
end;
end;
end.
> 200 to adapt it to your needs can be set.
if DBGrid1.Columns[i].Field.DisplayWidth > 200 then AutoColumnWidth.Index:=i;
I hope this helps a little, or may lead into the right direction.