Link to home
Start Free TrialLog in
Avatar of Toutou la bella
Toutou la bella

asked on

Color StringGrid in delphi 7

Hello, I want to color every cell of a stringgrid without using the OnCellDraw event in a button for example is there a solution for that? I m using Delphi 7 and sorry for my bad english  Thanks.
Avatar of Geert G
Geert G
Flag of Belgium image

the OnCellDraw is specifically created to draw custom contents
how would you do it otherwise ?

what are you trying to do ?
Avatar of Toutou la bella
Toutou la bella

ASKER

I want to do a cycle of activity with stringgrid with different durations and each cell has a different type (ie a different color) (I put an example of a cycle in the photo)User generated image but if i clicked on the scrollbars the colors disappear this is the problem ,this is the code:
procedure TForm1.Button1Click(Sender: TObject);
var
counter1,counter2:integer;
 i, W, WMax,S,j: integer;
StringGridCanvas: TControlCanvas;
StringGridRect : TRect;
ACol, ARow: Integer;
  Rect: TRect;
begin
StringGridCanvas:= TControlCanvas.Create;
       StringGridCanvas.Control:= TStringGrid(sender);
       StringGridRect.Right := TStringGrid(sender).Width;
       StringGridRect.Bottom := TStringGrid(sender).Height;
S:=0;
CalculOuvraison.First;
while not (CalculOuvraison.Eof) do
begin
S:=S+1;
CalculOuvraison.next;
end;
StringGrid1.ColCount:=S;
 WMax := 0;
  for i := 0 to (StringGrid1.RowCount - 1) do begin
    W := StringGrid1.Canvas.TextWidth(StringGrid1.Cells[0, i]);
    if W > WMax then
      WMax := W;
  end;
j:=0;
CalculOuvraison.First;
while not (CalculOuvraison.Eof) and (j<> S)do
begin
StringGrid1.ColWidths[j] := (CalculOuvraison.FieldByName('Duree').Value*10);
StringGrid1.Cells[j,0]:= CalculOuvraison.FieldByName('Type').Value;
j:=j+1;
CalculOuvraison.Next;
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  var
  j:integer;
begin
For j:=0 to Stringgrid1. ColCount do
begin
if (Stringgrid1.Cells[j,0]='F') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := clGreen;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end
else if (Stringgrid1.Cells[j,0]='P') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := $B469FF;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end
else if (Stringgrid1.Cells[j,0]='Q') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := clBlue;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end
else if (Stringgrid1.Cells[j,0]='M') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := clRed;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end
else if (Stringgrid1.Cells[j,0]='0') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := clBlack;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end
else if (Stringgrid1.Cells[j,0]='R') and(Acol=j) and (ARow=0 )then
begin
     with TStringGrid(Sender) do
     begin
      Stringgrid1.Cells[j,0]:='';
      Canvas.Brush.Color := $00A5FF;
      Canvas.FillRect(Rect);
      Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
     end;
end;
end;
end;

Open in new window

you are changing data within that event ?
shouldn't do that

set the data separately

in the ondrawcell, only draw the content of that cell
sorry can you explain more please
the OnDrawCell is called when painting a cell ... for all the cells in the grid

in the header of the event, you see ACol, ARow:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);

that indicates for what cell the event is called
ugh ???

procedure TForm1.Button1Click(Sender: TObject);
 ...
begin
  ...
       StringGridCanvas.Control:= TStringGrid(sender);

The Sender is Button1 ... so how do you want to convert a Button to StringGrid ?


i'm trying to rewrite your code ... give me a few secs
you can replace this :
CalculOuvraison.First;
while not (CalculOuvraison.Eof) do
begin
S:=S+1;
CalculOuvraison.next;
end;

Open in new window


with this:
S := CalculOuvraison.RecordCount;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium 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
I m realy very thankful thank youuuuu