Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coloring a cell in a StringGrid

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
Hey Guys,

Is there any easy way of coloring a cell in a StringGrid?

This is what i want is as follows...
If the 2 if statements are both are successful then, into the Cell [a,b] the "XXXX" text will be inserted and the Cell will have to colored in clGreen.

Code:
for a := 6 to 33 do
  for b := 1 to SRows do
  begin
    If StrToDate(ScheduleGrid.Cells[a,0]) >= StrToDate(ScheduleGrid.Cells[4,b]) then
      If ScheduleGrid.Cells[a,0] <= ScheduleGrid.Cells[5,b] then
        ScheduleGrid.Cells[a,b] := 'XXX';
  end;

Any ideas?

Thanks in advance...

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
faq102-3514 describes how to modify the appearance of a StringGrid cell. Use the same idea to color the cell background when a certain condition occurs.

Andrew
Hampshire, UK
 
OK.

But i have no idea how to edit that code to help me.

Sorry, but i'm new to this coloring of cells and StringGrid's in general.

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
This is the code from the FAQ. It is used to right align the text in the StringGrid cell.
Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  text: string;
  WidthOfText: integer;
  WidthOfCell: integer;
  LeftOffset: integer;
begin
 with StringGrid1 do begin
  text := Cells[ACol,ARow];
  WidthOfText := Canvas.TextWidth(text);
  WidthOfCell := ColWidths[ACol];
  LeftOffset := WidthOfCell - WidthOfText;
  Canvas.TextRect(Rect,Rect.Left+LeftOffset,Rect.Top,text);
 end;
end;
Delete the three lines beginning WidthOfText := .. and replace them with code to change the background color. It is impossible to be precise because I don't know enough about the application (e.g. is SRows a constant? Is the grid updated in real time and so on)
Code:
  ...
  if SomeConditionThatYouCode then
    Canvas.Brush.Color := clGreen;
  else
    Canvas.Brush.Color := clWhite;
  ...

Andrew
Hampshire, UK
 
Did you do a google search first? I found some appropriate pages within a few seconds:

Did you search this forum? I found at least two entries for colouring aspects of a TStringGrid:
thread102-959639
thread102-649955

Please do go through these steps before posting a new thread.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Just in case, heres a couple of very basic points.

ondrawcell event templete code is produced by double clicking on the ondrawcell entry of the stringgrid object inspector events page.

Also note that a call to yourstringgridname.repaint will force a call to ondrawcell if you need to update immediatly.



Steve: Delphi a feersum engin indeed.
 
here's an OnDrawCell I use that if the value is between certain critera I color the ROW:

Code:
procedure TfrmMain.sgStatusCounterDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  With Sender as TStringGrid do
  begin
    if cells[3, ARow] <> '' then
    begin
      if IsNumeric(cells[3, ARow]) then
      begin
        if StrToInt(cells[3, ARow]) >= 90 then
        begin
          //for BackGround  Color
          Canvas.Brush.Color := clRed;
          Canvas.FillRect(Rect);
          //for Font.Color
          Canvas.Font.Color := clBlack;
          Canvas.TextRect( Rect, Rect.Left+2, Rect.Top+2, Cells[acol, arow]);
        end
        else if (StrToInt(cells[3, ARow]) >= 85) and (StrToInt(cells[3, ARow]) <= 89) then
        begin
          //for BackGround  Color
          Canvas.Brush.Color := clYellow;
          Canvas.FillRect(Rect);
          //for Font.Color
          Canvas.Font.Color := clBlack;
          Canvas.TextRect( Rect, Rect.Left+2, Rect.Top+2, Cells[acol, arow]);
        end
        else if (StrToInt(cells[3, ARow]) > 80) and (StrToInt(cells[3, ARow]) < 85) then
        begin
          //for BackGround  Color
          Canvas.Brush.Color := clSkyBlue;
          Canvas.FillRect(Rect);
          //for Font.Color
          Canvas.Font.Color := clBlack;
          Canvas.TextRect( Rect, Rect.Left+2, Rect.Top+2, Cells[acol, arow]);
        end;
      end;
    end;
  end;
end;

I think you will need to change the FillRect(Rect) with the ACol and ARow variables

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top