Solved

StringGrid

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calling Tcolordialog in dll make the form disappear 8 103
code issue 8 99
delphi parse string to params 3 97
Tviruailstringtree sort multi columns on header click 1 52
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

19 Experts available now in Live!

Get 1:1 Help Now