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

LVL 4
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveBayCommented:
Something like this:

Canvas.TextRect( rect, rect.left, rect.Top + 2, FormatCurr('0.00',FieldByName( Field.FieldName).AsFloat));
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAuthor Commented:
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......
0
mikelittlewoodCommented:
or

Canvas.TextRect( rect, rect.left, rect.Top + 2, FieldByName( Field.FieldName).AsCurrency);
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

SteveBayCommented:
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?
0
SteveBayCommented:
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...
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAuthor Commented:

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] ) );
0
SteveBayCommented:
>>  [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...
0
senadCommented:
why not just make cells currency type (field type currency) ?
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAuthor Commented:
senad:

That's the point, they are already (field type currency). Until the color is applied.
0
mikelittlewoodCommented:
Doh forgot that Steve. Guess it could just be wrapped inside a currToStr function too then.
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerAuthor Commented:
Ok, got it from http://codenewsfast.com/isapi/isapi.dll/article?id=C9B3976D&article=7031968

Knew it had to be fairly simple;


procedure TEstimatedWipCostsf.wwDBGrid1CalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; Highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  with (Sender as TwwDBGrid), cdsEstimatedWipCosts do begin
    if (Field.Value < 0) then
      ABrush.Color := 8421631
    else
      ABrush.Color := 8454016;
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveBayCommented:
Ah, I see. You where using the wrong event. I never would have guessed that without installing the WW grid component and I'm already flush with grids that I don't use as it is.
0
mikelittlewoodCommented:
You could always change to use the virtual string tree/grid (cant remember its name) from soft gems.
That has a good tree search facility.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.