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.
Toutou la bellaAsked:
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.

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

what are you trying to do ?
0
Toutou la bellaAuthor Commented:
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)test.PNG 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

0
Geert GOracle dbaCommented:
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Toutou la bellaAuthor Commented:
sorry can you explain more please
0
Geert GOracle dbaCommented:
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
0
Geert GOracle dbaCommented:
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
0
Geert GOracle dbaCommented:
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

0
Geert GOracle dbaCommented:
i wrote a different sample:

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    fGridData: TGridData;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var I, J: Integer;
begin
  StringGrid1.RowCount := cGridRows+1;
  StringGrid1.ColCount := cGridCols+1;
  for I := 0 to cGridRows do
    for J := 0 to cGridCols do
      StringGrid1.Cells[J, I] := fGridData[I, J];
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  n: Integer;
  c: TColor;
begin
  With TStringGrid(Sender) do
  begin
    n := 0;
    if Cells[ACol, ARow] <> '' then
      n := StrToInt(Cells[ACol, ARow]);
    c := clWhite;
    if n > 4000 then
      c := clGreen
    else if n > 3000 then
      c := clYellow
    else if n > 2000 then
      c := clOlive
    else if n > 1000 then
      c := clLime;
    Canvas.Brush.Color := c;
    Canvas.FillRect(Rect);
    Canvas.Textout(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
  end;
end;

end.

Open in new window


in the onDrawCell, you should only put the drawing logic
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
Toutou la bellaAuthor Commented:
I m realy very thankful thank youuuuu
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.