A small program was developed recently. SQLite was used, and the analysis program written in Python in the background was used to insert (update) data into the SQLite database. The Delphi program cycle showed the database content. Delphi accesses the aducom component used by SQLite. Python inserted data encoding is the use of the UTF-8, And the Delphi DBGrid, cxgrid control display is garbled, mainly because Delphi7 does not support Unicode caused, so we want to make it support.
I tried a variety of methods, including the use of TMS Unicode component and suipack that are said to support Unicode. Finally, a simple method is used. In the ongettext event of the fields to be displayed in the dataset component, Unicode to GB conversion is performed on the data during event processing.
procedure unicode2gb(const unicodestr:string; var GbStr:String);var SourceLength:integer; DoneLength:integer; AscNo:integer; Byte1,Byte2,Byte3:integer;begin GbStr:=''; if Trim(unicodestr)='' then exit; SourceLength:=Length(UnicodeStr); DoneLength:=1; repeat AscNo:=ord(UnicodeStr[DoneLength]); case (AscNo and $E0) of $E0:begin Byte1:=(AscNo and $0f) shl 12; Inc(DoneLength); if DoneLength>SourceLength then break; AscNo:=ord(UnicodeStr[DoneLength]); Byte2:=(AscNo and $3f) shl 6; Inc(DoneLength); if DoneLength>SourceLength then break; AscNo:=ord(UnicodeStr[DoneLength]); Byte3:=AscNo and $3f; end; $C0:begin Byte1:=(AscNo and $1f) shl 6; Inc(DoneLength); if DoneLength>SourceLength then break; AscNo:=ord(UnicodeStr[DoneLength]); Byte2:=(AscNo and $3f); Byte3:=0; end; 0..$bf:begin Byte1:=AscNo; Byte2:=0; Byte3:=0; end; end;//case; GbStr:=GBStr+widechar(Byte1+Byte2+Byte3); Inc(DoneLength); if DoneLength>SourceLength then break; until DoneLength>=SourceLength;end;
In addition, when you use cxgrid for display, you need to set the color based on the field value. This can be handled in the ongetcontentstyle event of styles in tableview, as shown below:
procedure TFormMain.cxGrid1DBTableView1StylesGetContentStyle( Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);var attention : integer;begin attention := ARecord.Values[5]; if attention > 0 then begin AStyle := styleAttention; exit; end; if ARecord.Values[4] = 0 then begin AStyle := styleRed; end else begin AStyle := styleDefault; end;end;
Styleattention and styledefault are various styles set in cxstylerepository1.