Link to home
Start Free TrialLog in
Avatar of enshrentko
enshrentko

asked on

how to show separate digit in delphi 7 dbgrid

hi,

i need to separate the number contains in dbgrid (e.g. 45.000.500 instead of 45000500)

how can i do this in delphi 7?

thanks
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

function AddPointEvery3Digits(S:String):String;
Var
 L:integer;
begin
 Result:='';
 L:=Length(S);
 While L>3 do
  begin
   Result:='.'+Copy(S,L-2,3)+Result;
   L:=L-3;
   S:=Copy(S,1,L);
  end;
 Result:=S+Result;
end;
add the fields to query --> persistent fields
and set the displayformat in the fields to 0,0

or set at runtime
query.fieldbyname('fieldname').DisplayFormat := '0,0';
only works for numeric fields
and set the
thousandseparator := '.';

in your app
ah, yes it's a DBGrid.
Check this solution :

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
          const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
   st : string;
   sv : variant;
   si : integer;
begin
   with (Sender as TDBGrid), Canvas do
      begin
      if Column.Fieldname = 'my_field_name'  then
         begin
         sv := Column.Field.Value;
         if not (VarIsNull(sv)) then
            begin
            sq := Column.Field.Value;
            if sq < 0 then sq := sq * -1;
            if (sq = 0) or (sq < 0.01) then
               begin
               FillRect(Rect);
               Exit;
            end;
            st := FormatFloat('###.###.###,##;;-', Column.Field.Value);
            if st[1] = ',' then  st := '0' + st
            else  if pos('-,', st) > 0 then  st := '-0' + copy(st,2,10);
            si := round(((Rect.Right - Rect.Left) / 2)) - round(TextWidth(st) / 2);
            TextRect(Rect, Rect.Left + si, Rect.Top+2, st);
         end;  
      end;      
      DefaultDrawColumnCell(Rect, DataCol, Column, State);
   end;  
end;
Oops, Exit missing after line "TextRect........."
ASKER CERTIFIED SOLUTION
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial