Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to color a StringGrid row

1,877 views
Skip to first unread message

Doug Maurer

unread,
Jun 13, 2003, 1:00:03 PM6/13/03
to
Delphi 7 enterprise

I need to change the color of an entire row in a string grid depending on
the value of one of the cells. Ex. if the field called DQ has a value of
true, then I need that entire row to be red.

Please advise.

TIA

Doug Maurer


Eduardo Andrade Bahiense

unread,
Jun 13, 2003, 1:09:00 PM6/13/03
to
Use the OnDrawCell event
with Sender as TstringGrid do
begin
if SomeCondition then
begin
//for BackGround Color
Canvas.Brush.Color := Somecolor;
Canvas.FillRect(Rect);
//for Font.Color
Canvas.Font.Color := SomeColor;
Canvas.TextOut, or Canvas.TextRect or
DrawText(Canvas.Handle,... as you wish to
output Cells[acol,arow]
//aRow and aCol are normal parameters
end;
end
cheers, Eduardo

"Doug Maurer" <dma...@acetrucking.com> escreveu na mensagem
news:3eea0321$1...@newsgroups.borland.com...

Doug Maurer

unread,
Jun 13, 2003, 1:29:07 PM6/13/03
to
This is what I have and it changes the color of the whole row and the
previous row I was on and you can't read any text.

What am I missing?

with Sender as TDBAdvStringGrid do
begin
if dmDatamod.resultQryDS.fieldbyname('DQ').asboolean then
begin
canvas.Brush.color:= clRed;
canvas.fillrect(rect);
canvas.Font.color:= clWhite;
canvas.TextRect(rect,arow,acol,Text) ;
end;
end;

.
"Eduardo Andrade Bahiense" <edu...@fdv.com.br> wrote in message
news:3eea...@newsgroups.borland.com...

Eduardo Andrade Bahiense

unread,
Jun 13, 2003, 1:46:49 PM6/13/03
to
ok. I was talking about TStringGrid.
In a TDBStringGrid you must use OnDrawDataCell Event

In the case of your code, if the object you are using doesn't sinchronize
with DataSource, then the behavior you are getting is expected, since the
value of a given field is always the same, regardless the row you are in the
object.

if the object you are using does not update the cursor of your datasource,
then try to do it by yourself before checking the value of a field.
One manner to acomplish this, if the rowcount of your grid is the same of
the recordcount ofyour dataset is
to set the Recno Property the same of Row:

DataSet.Recno := aRow;
Check the condition and set the color;

Hope that helps. I'm leaving for weekend

Eduardo

"Doug Maurer" <dma...@acetrucking.com> escreveu na mensagem

news:3eea09fd$1...@newsgroups.borland.com...

0 new messages