Link to home
Start Free TrialLog in
Avatar of sguerra
sguerra

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of mocarts
mocarts

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
Avatar of mocarts
mocarts

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.
Avatar of sguerra

ASKER

Thanks mocarts, it worked perfectly
don't forget to accept answer ;)
wbr, mo.