How to format a DBGrid column to display two decimal places?

I would like to format specific cells to force two decimal places. Data comes from the ElevateDB stored procedure and connects to the TDataSource.

EDIT: SQL Programming Note:

I was not sure if this was just ElevateDB's problem or not. Before learning about the Field Editor , I tried to format the data at the SQL level using the CAST (NumericField as varchar (10)) statement inside the stored procedure. In doing so, he did not find the DisplayFormat property inside the field editor for this particular field.

When I removed the CAST () statement from the stored procedure, the DisplayFormat property appeared in the Field Editor .

+7
source share
3 answers

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

+9
source

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

  TFloatField(MyDs.FieldByName('Cost')).DisplayFormat := '0.00'; 
+7
source

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; 
+2
source

All Articles