Calvin Day
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?
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;
ASKER
SteveBay,
Tried that, doesnt work.
Tried these Also;
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa me).AsFloa t]));
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa me).asCurr ency]))
This works for the formatting, but loses the color.
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa me).Value] ))
This is weird......
Tried that, doesnt work.
Tried these Also;
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa
This works for the formatting, but loses the color.
Canvas.TextRect( rect, rect.left, rect.Top + 2, Format('%.2f', [FieldByName(Field.FieldNa
This is weird......
or
Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrenc y);
Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrenc
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?
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.FieldBy Name 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...
Rather than using the ClientDataSetCosts.FieldBy
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...
ASKER
This; Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrenc
produces this; [Error] EstimatedWipCostsu.pas(213
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...
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) ?
ASKER
senad:
That's the point, they are already (field type currency). Until the color is applied.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Canvas.TextRect( rect, rect.left, rect.Top + 2, FormatCurr('0.00',FieldByN