[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

StringGrid

Posted on 2000-05-16
4
Medium Priority
?
196 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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