?
Solved

StringGrid

Posted on 2000-05-16
4
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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