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.
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.
Since you've said you can't do it on the TField level or in your Delphi code, you can use Trim() or Left(), depending on the database you're using, on the column to remove the blank spaces you don't want to display:
If the column type is really varchar(255) and not char(255) then there shouldn't be any spaces in the end of the value unless you saved the spaces yourself. IOW the varchar(255) values will be returned to you exactly as you saved them into the table whether char(255) values will be right padded with spaces so that you always get string with 255 characters.
Thus I'd say that if you use varchar type and you don't pad the values with spaces when saving them into the DB then the problem is really in the grid and you can't solve it usimg trim() or equivalent in SQL. Most likely the grid reads the field's definition, sees that the max length of the value is 255 characters and sets the column width accordingly. So the solution is really to set the column width manually or use column's ApplyBestFit method (if it has one).
There are two ways of limiting the length of a field displayed in a DBGrid that do not alter the data from the database. One is by setting the DisplayWidth of the field and the other is setting the Width of the grid column.
b1e95dc632