Let’s say I have this table:
name varchar(255),
quantity integer,
value float
When I used this code:
select left(name, 99), quantity, value from table;
But I’ve got all the 255 spaces (by space I mean width length, not the real space chars) on the first column of the dbgrid and the other two fields where away unless I used the scroll bar and my objective was to make scroll bars not necessary.
How do I get this goal correctly? I use Firebird and MySQL.
I can not do this by pascal code nor define it on the Fields object of the table component because this codes comes from many tables as this is a report generator and the first field is not always the same and for each report I have to use a different SQL code.

PLEASE, EVERYBODY READ THE ALL QUESTION BEFORE ANSWERING. THE FIRST 3 ANSWER WHERE WRONG BECAUSE THEY DID NOT CONSIDERED WHAT AS WRITTEN IN THE LAST PARAGRAPH!
Since you’ve said you can’t do it on the
TFieldlevel or in your Delphi code, you can useTrim()orLeft(), depending on the database you’re using, on the column to remove the blank spaces you don’t want to display:For instance, in many SQL dialects you could use something like:
Some allow this instead:
or
As a quick test (I don’t have FB, but have SQL Server 2008, so I used it), I created the following test data in SQL Server Management Studio:
I then created a new D2007 VCL Forms application, and performed the following steps:
TADOConnectionon the form (ADOConnection1), and set up the connection string to the test data aboveTADOQuerycomponents on the form (ADOQuery1andADOQuery2), and set theirConnectionproperties toADOConnection1TDataSourcecomponents on the form (DataSource1andDataSource2) and connected theirDataSetproperties to the matchingADOQuery1andADOQuery2TDBGridcomponents (DBGrid1andDBGrid2) and set theirDataSourceproperties to the matchingDataSource1andDataSource2.SELECT * FROM testto the SQL forADOQuery1SELECT Substring(itemname, 1, 30) as itemname, quantity, value FROM testas theSQLofADOQuery2. The length of30was arbitrarily chosen as being a small enough value to demonstrate the considerably smaller column width.ADOConnection1.ConnectedtoTrue, and set bothTADOQuery.Activeproperties toTrue.The above produced this output (at design time, without ever compiling or running the application), with the top grid being
DBGrid1:As you can see, in
DBGrid2, which is connected toADOQuery2with theSubstringin the SQL, the first column is correctly sized to 30 characters width.