Limit Decimal Places in DBGrid

Tag: delphi , grid Author: wrhwrhwrhwr Date: 2010-08-30

I have a DBGrid which is connected to a ClientDataSet and it has a field UnitCost. What I want to do is to limit to 2 decimal places every value that will be assigned to that field. Is there an easier way to do this other than validating every input value?

Best Answer

You can try to set ClientDataset.FieldByName('UnitCost').EditMask := '#.00';

comments:

+1 for the EditMask as the OP seems interested in limiting input values (as opposed to just formatting the values for display).
How can I ged rid of the underscores and '.' in the grid because the grid by default has a value of _.?

Other Answer1

I usually use:

TFloatField(Myds.FieldByName('MyField')).DisplayFormat := '#.##';

This should work with every type of TDataset

Other Answer2

procedure TFMasterReport.DBDetail**DrawColumnCell**(Sender: TObject;
   const Rect: TRect; DataCol: Integer; Column: TColumn;
   State: TGridDrawState);
begin
    TFloatField(Tempdtl.FieldByName('TS_Rate')).DisplayFormat := '0.000';
    TFloatField(Tempdtl.FieldByName('TS_Tvol')).DisplayFormat := '0.00';
    TFloatField(Tempdtl.FieldByName('TS_permit')).DisplayFormat := '0.00';
    TFloatField(Tempdtl.FieldByName('TS_tax')).DisplayFormat := '0.00';
    TFloatField(Tempdtl.FieldByName('TS_nettval')).DisplayFormat := '0.00';
end;