Recent

Author Topic: See German characters in a dbgrid  (Read 427 times)

carlobo

  • Newbie
  • Posts: 2
See German characters in a dbgrid
« on: January 21, 2020, 04:44:53 pm »
Hi, I have a dbf table which contains some fields with characters of the German alphabet, those with the umlaut (äüö ...). I place a dbgrid and in Delphi I see the characters correctly, while with Lazarus 2.0.4 / FPC 3.0.4 strange characters appear.
There is a solution ?
Thank you

wp

  • Hero Member
  • *****
  • Posts: 6704
Re: See German characters in a dbgrid
« Reply #1 on: January 21, 2020, 05:04:15 pm »
Here is an example which I posted probably here and which has CP850-encoded strings in a dbf file. The code uses the OnGetText and OnSetText events of TField to convert from this encoding to/from UTF8. Adapt the conversions to the encoding found in your file.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 3296
Re: See German characters in a dbgrid
« Reply #2 on: January 21, 2020, 05:15:54 pm »
The dBase file format dates from the 1980s and all early dBase files should be treated as DOS files encoded with the US ASCII code page (often  CP 437).
Here's an alternative approach to wp's:

After calling Open on a TDBF file, you have to set the field's Transliterate property to True.
Code: Pascal  [Select]
  1. for i := 0 to DBFFile.Fields.Count-1 do
  2.   if DBFFile.Fields[i] is TStringField then
  3.     TStringField(DBFFile.Fields[i]).Transliterate := True;

Then you have to write an OnDBFFileTranslate event handler:
Code: Pascal  [Select]
  1. function TForm1.DBFFileTranslate(Dbf: TDbf; Src, Dest: PChar; ToOem: Boolean): Integer;
  2. var  s: String;  len: Integer;
  3. begin  s := StrPas(Src);
  4.   len := Length(s);
  5.   if len = 0 then
  6.     Exit;
  7.   s := ConvertEncoding(s, 'CP437', EncodingUTF8);  // adjust to suit the actual encoding of your dBase data
  8.   if Length(s) > len then
  9.     SetLength(s, len);
  10.   Move(s[1], Dest^, Len+1);
  11.   Result := len;
  12. end;
« Last Edit: January 21, 2020, 07:11:28 pm by howardpc »

carlobo

  • Newbie
  • Posts: 2
Re: See German characters in a dbgrid
« Reply #3 on: January 21, 2020, 09:24:52 pm »
Hi howardpc,
Your solution works properly, thanks.
I have the same problem in another project which instead of the dbf tables uses mysql tables which I access with TSQLQuery. Also in this case there is a solution? Thank you