Link to home
Start Free TrialLog in
Avatar of Calvin Day
Calvin DayFlag for United States of America

asked on

TwwDBGrid loses currency format.

The code sample changes the cell color for positive and negative currency amounts and works well.
However the currency formatting is lost, dollar signs and thousands separators are getting stripped off.
What is an elegant way to stop or fix this behavior?

procedure TCostsf.wwDBGrid1DrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
begin
 
  with (Sender as TwwDBGrid), ClientDataSetCosts do begin
    if (Field.Value < 0) then
      Canvas.Brush.Color := 8421631
    else
      Canvas.Brush.Color := 8454016;
 
    Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsString);
 
  end;
end;

Open in new window

Avatar of SteveBay
SteveBay
Flag of United States of America image

Something like this:

Canvas.TextRect( rect, rect.left, rect.Top + 2, FormatCurr('0.00',FieldByName( Field.FieldName).AsFloat));
Avatar of Calvin Day

ASKER

SteveBay,

Tried that,  doesnt  work.

Tried these Also;
   Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldName).AsFloat]));
   Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldName).asCurrency]))

This works for the formatting, but loses the color.
    Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldName).Value]))

This is weird......
or

Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrency);
Mike,
 TField.AsCurrency does not return a string therefore your example would not compile.

Joe,
That is odd. Once you set brush color it should stay set until you change it. Why does .Value work while .AsFloat and .AsCurrency do not? What is the datatype?
Another thought:
Rather than using the ClientDataSetCosts.FieldByName have you tried just using:

Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [Field.Value] ) );

I don't think it should make any difference but you never know...

This; Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrency);
produces this; [Error] EstimatedWipCostsu.pas(213): Incompatible types: 'String' and 'Currency'

This formats the value correctly but loses the color;
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [Field.Value] ) );
>>  [Error] EstimatedWipCostsu.pas(213): Incompatible types: 'String' and 'Currency'
That's right. TextRect is looking for a string value in that parameter.
I still don't get the loss of color...
why not just make cells currency type (field type currency) ?
senad:

That's the point, they are already (field type currency). Until the color is applied.
Doh forgot that Steve. Guess it could just be wrapped inside a currToStr function too then.
ASKER CERTIFIED SOLUTION
Avatar of Calvin Day
Calvin Day
Flag of United States of America 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
SOLUTION
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
SOLUTION
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