Solved

StringGrid

Posted on 2000-05-16
4
192 Views
Last Modified: 2010-04-04

Can you change the font, font color, for each of the rows or cells in a string grid or you necessarily need a customed component?
0
Comment
Question by:vdelgado
  • 2
4 Comments
 
LVL 3

Expert Comment

by:shenqw
ID: 2815510
I think you must draw the stringgrid cell yourself. use the OnDrawCell.
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 2815529
yes must ownerdraw them to change colors of rows or columns..

simple example:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with Sender as TStringGrid do
  begin
    if ACol = 1 then
      Canvas.Font.Color := clRed
    else
      Canvas.Font.Color := clBlack;
    Canvas.TextRect(Rect, Rect.Left, Rect.Top, Cells[ACol, ARow]);
  end;
end;



or fuller example:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
      Rect: TRect; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Longint;
  Rect: TRect; State: TGridDrawState);
var
  FocusedRect : TRect;
begin
  FocusedRect := Rect;
  StringGrid1.Canvas.Font.Color  := clBlack;
  StringGrid1.Canvas.Pen.Color := clBlack;
  StringGrid1.Canvas.Pen.Width := 1;
  if ( gdFocused in State ) then begin
    StringGrid1.Canvas.Font.Style  := [fsBold];
    StringGrid1.Canvas.Font.Color  := clRed;
    StringGrid1.Canvas.Brush.Color  := clblue;
  end
else
    StringGrid1.Canvas.Font.Style  := [];
  InflateRect(FocusedRect, -2, -2);
  StringGrid1.Canvas.FillRect(FocusedRect);
   if ( gdSelected in State ) then
    StringGrid1.Canvas.TextOut(Rect.Left+8, Rect.Top+2, StringGrid1.Cells[Col,Row])   else
    StringGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[Col,Row])
 end;

end.
0
 

Author Comment

by:vdelgado
ID: 2817601
The answer proposed by Inthe is good as far as you define the color and font of a selected cell, but what I want to do is to define a specific color and/or font for the cells as I am filling the grid without the need to select a specific cell.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2819294
you probably just want to do something like this then :

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with Sender as TStringGrid do
  begin
  if (ACol = 0)  then
      Canvas.Brush.Color := clActiveCaption
    else
    if  arow = 0 then
      Canvas.Brush.Color := clActivecaption
    else
    if (ACol = 1) and (arow = 2) then
      Canvas.Brush.Color := clRed
    else
    if (ACol = 2) and (arow = 3) then
      Canvas.Brush.Color := clgreen
    else
    if (ACol = 2) and (arow = 1) then
      Canvas.Brush.Color := clFuchsia
    else
    if (ACol = 1) and (arow = 3) then
      Canvas.Brush.Color := clAqua
    else
    if (ACol = 2) and (arow = 4) then
      Canvas.Brush.Color := clYellow
    else
      Canvas.Brush.Color := clblue;
    Canvas.TextRect(Rect, Rect.Left, Rect.Top, Cells[ACol, ARow]);
  end;
end;

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question