Re: color cell in delphi stringgrid

Posted by delphiman on October 21, 2005

In Reply to: color a cell in stringgrid posted by Sammy on October 16, 2005

: Hi all,
: I have learned how to use ..DrawCell event to color an individual cell in the stringgrid.

: But how can I colour a cell, that is, to send the row and column variables (say, 3rd row and 4th column) from a procedure by clicking on a button?

: thanks,
: Sammy

Location of colored cell:

private
  ColorCol, ColorRow: integer;

Set colored cell column and row with Button1, and force stringgrid to repaint:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ColorCol := 4;
  ColorRow := 3;
  StringGrid1.Repaint;
end;

Color delphi stringgrid cell in OnDrawCell event handler:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: string;
begin
  if (ACol = ColorCol) and (ARow = ColorRow) then begin
    StringGrid1.Canvas.Brush.Color := clYellow;
    StringGrid1.Canvas.FillRect(Rect); 
    S := StringGrid1.Cells[ACol, ARow]; 
    StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, S);  
  end;
end;

good luck,
DelphiMan

Related Articles and Replies

Annoying StringGrid Focus
... setting the focus on the upper lefthand cell [0,0] and coloring it dark blue.This obfuscates what i want to show in that cell...

a grid with FG/BG setable colors
... setting the focus on the upper lefthand cell [0,0] and coloring it dark blue.This obfuscates what i want to show in that cell...

Annoying StringGrid Focus
... setting the focus on the upper lefthand cell [0,0] and coloring it dark blue.This obfuscates what i want to show in that cell...