Solved

StringGrid

Posted on 2000-05-16
4
188 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now