8

I would like to format specific cells to force two decimal places. The data is coming from an ElevateDB stored procedure and hooked into a TDataSource.

EDIT: SQL Programming Note:

I wasn't sure if this was just an ElevateDB issue or not. Before knowing about the Fields Editor, I attempted to format the data at the SQL level by using a CAST (NumericField as varchar(10)) statement inside the stored procedure. By doing so, it did not expose the DisplayFormat property inside the fields editor for this particular field.

When I removed the CAST() statement from the stored procedure, the DisplayFormat property showed up in the Fields Editor.

3 Answers3

9

You can format the DBGrid columns by formatting the underlying fields. If not done, create static fields in your dataset and then set the DisplayFormat property of the field in question to 0.00 and you are done.

Uwe Raabe
  • 39,888
  • 3
  • 77
  • 115
9

I use same method as Uwe answered, in code after opening the dataset just add this line of code to format certian column:

  TFloatField(MyDs.FieldByName('Cost')).DisplayFormat := '0.00';
Mohammed Nasman
  • 10,782
  • 7
  • 41
  • 67
2

You can format the field using the DrawDataCell event.

procedure TFormMain.DBGridCompareDrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
begin
  if Field.Name = 'FIELDNAME' then
    TFloatField(Field).DisplayFormat := '#,##0.00';
end;
Pieter van Wyk
  • 2,268
  • 9
  • 41
  • 63