Recent

Author Topic: Problem - space characters added behind text in DBGrid  (Read 5630 times)

Tom83

  • New member
  • *
  • Posts: 6
Problem - space characters added behind text in DBGrid
« on: November 25, 2010, 09:18:08 am »
Hi,
I have a Firebird database (using UTF8 encoding). I connect to the database with TIBConnection component and I set TIBConnection.CharSet to 'UTF8'. Now I have the following problem with displaying values from char(n) columns:
In the database I have a column:

code char(3)

I insert a value to this column, e.g. 'ABC'. When I display the value of this column in FlameRobin, it works correctly, there is 'ABC'. But when I display the value in DBGrid or other database components, there is 'ABC         ', so it added 9 spaces behind the text.
The same problem occurs when I assign the value of the column to the variable:

var A: string;
.
.
.
A:=SQLQuery1.FieldByName('Code').AsString;

In A there isn't 'ABC', but there is 'ABC         '. I know I can simply delete these unwanted spaces, but is there another solution (better and more comfortable) how to get rid of these additional spaces?

Lacak2

  • Guest
Re: Problem - space characters added behind text in DBGrid
« Reply #1 on: November 25, 2010, 10:32:31 am »
Can you look at SQLQuery1.FieldByName('Code').DataSize
what value it returns ?

Tom83

  • New member
  • *
  • Posts: 6
Re: Problem - space characters added behind text in DBGrid
« Reply #2 on: November 25, 2010, 10:43:52 am »
It returns 13.

Lacak2

  • Guest
Re: Problem - space characters added behind text in DBGrid
« Reply #3 on: November 26, 2010, 05:58:20 am »
May be, that 3*4+1=13 (
 3-as character size of column char(3)
 4-as max.count of bytes per character in UTF-8
 1-as null string terminator

char(3) field si mapped to ftFixedChar so trailing blanks are not removed.
In case of varchar(3) trailing blanks will not present (I hope ;-)).

You can remove these unwanted spaces using TField.OnGetText and there use trim()

Tom83

  • New member
  • *
  • Posts: 6
Re: Problem - space characters added behind text in DBGrid
« Reply #4 on: November 28, 2010, 04:36:08 pm »
Thank you, I think it will work, but I need more information :).
My idea was to write a procedure:

Code: [Select]
procedure TForm1.DeleteSpaces(Sender: TField; var aText: string; DisplayText: Boolean = true);
begin
  aText:=Trim(Sender.AsString);
end;

Then to assign this procedure as an event handler for all the fields that are of ftFixedChar DataType:

Code: [Select]
for I:=0 to SQLQuery1.FieldCount-1 do
  if SQLQuery1.Fields[I].DataType=ftFixedChar then
    SQLQuery1.Fields[I].OnGetText:=@DeleteSpaces;

But I don't know where to put this code. When the SQLQuery is inactive, there are no fields, so I can't assing them the OnGetText event handler. I tried to put the code to SQLQuery.AfterOpen event handler, but it still doesn't work. Probably because the fields are already filled with data, so the event OnGetText was already executed.
Or is there something wrong with DeleteSpaces procedure?

Lacak2

  • Guest
Re: Problem - space characters added behind text in DBGrid
« Reply #5 on: November 29, 2010, 07:36:36 am »
Your code seems ok, also AfterOpen is right place.
OnGetText is called when component request data for displaying or editing.
Can you test if procedure DeleteSpaces is realy called ... may be, when you scroll in DBGrid ?

Tom83

  • New member
  • *
  • Posts: 6
Re: Problem - space characters added behind text in DBGrid
« Reply #6 on: November 30, 2010, 10:44:30 pm »
OK, now it works, the problem was that the Fields.DataType of char(n) columns was not ftFixedChar, but ftString.

 

TinyPortal © 2005-2018