Colored StringGrid

Hi
I need for my application a stringgrid with alternative colors in each row, but my grid needs to be in a goAlwaysShowEditor := True codition. But when a cell is being edited turns white with black text. I got something like this:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

if (gdFocused in State) then begin
  if (Arow mod 2 = 1) then begin
    StringGrid1.Canvas.Brush.Color := clYellow;
    StringGrid1.Canvas.Font.Color := clMaroon;
  end
  else begin
    StringGrid1.Canvas.Brush.Color := clWhite;
    StringGrid1.Canvas.Font.Color := clBlack;
  end;
end
else
  if (Arow mod 2 = 1) then begin
    StringGrid1.Canvas.Brush.Color := clYellow;
    StringGrid1.Canvas.Font.Color := clMaroon;
  end
  else begin
    StringGrid1.Canvas.Brush.Color := clWhite;
    StringGrid1.Canvas.Font.Color := clBlack;
  end;

If (ARow>0) then
 begin
   StringGrid1.canvas.fillRect(Rect);
   StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
 end;
end;

I need that the cell that is being edited do not change color. Please help, I am looking for this solution on-line for 2 days now and there is only the same answer again and again State = [gdFocused], but that does not helps.
LVL 1
sguerraAsked:
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.

mocartsCommented:
type
  TMyStringGrid = class(TStringGrid)
  end;
  TMyInplaceEdit = class(TInplaceEdit)
  end;
function GetBrushColor(const Row: integer): TColor;
begin
 if (Row mod 2 = 1) then
   Result := clYellow
 else
   Result := clWhite;
end;
function GetPenColor(const Row: integer): TColor;
begin
 if (Row mod 2 = 1) then
   Result := clMaroon
 else
   Result := clBlack;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.Canvas.Brush.Color := GetBrushColor(ARow);
  StringGrid1.Canvas.Font.Color := GetPenColor(ARow);

  if (ARow>0) then
  begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;

end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  CanSelect := True;
  if assigned(TMyStringGrid(StringGrid1).InplaceEditor) then
    with TMyInplaceEdit(TMyStringGrid(StringGrid1).InplaceEditor) do begin
      Color := GetBrushColor(ARow);
      Font.Color := GetPenColor(ARow);
    end;
end;

there is a problem to set color when editor is created for first time (as SelectCell ocurs befor editor is created), but I haven't time at the moment to fix this.
maybe later :)

wbr, mo.
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
mocartsCommented:
solved :) instead of OnSelectCell use OnGetEditText event of TStringGrid.

procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
  ARow: Integer; var Value: String);
begin
  if assigned(TMyStringGrid(StringGrid1).InplaceEditor) then
    with TMyInplaceEdit(TMyStringGrid(StringGrid1).InplaceEditor) do begin
      Color := GetBrushColor(ARow);
      Font.Color := GetPenColor(ARow);
    end;
end;

wbr, mo.
0
sguerraAuthor Commented:
Thanks mocarts, it worked perfectly
0
mocartsCommented:
don't forget to accept answer ;)
wbr, mo.
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.