Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Different color in Stringgrid

Posted on 2000-02-19
6
Medium Priority
?
754 Views
Last Modified: 2008-02-20
How do i define a special color/font for each cell in a Stringgrid?
I want the text in a special cell to have a special color. If i click in it I want the text to become red.
0
Comment
Question by:slyna
[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
  • 2
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
fulvio_brasil earned 1400 total points
ID: 2538666
slyna,

Put this code on event OnDrawCell of the StringGrids that you want to control.

Change the StringGrid's property 'DefaultDrawing' to FALSE.

In this example I changed the color from Grid's Col 2 to Col 4, but you can make the test that you need.


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var gr: TStringGrid;
begin
  // The grid sender
  gr := (Sender as TStringGrid);

  // Make here the tests that you want
  // I modified just the color, but you can modify what you want about Font
  if (ACol>=2) and (ACol=<4)
    then gr.Canvas.Font.Color := clGray
    else gr.Canvas.Font.Color := clBlack;

  if (ARow=gr.Row) AND (ACol=gr.Col)
    then gr.Canvas.Font.Color := clRed;

  // Write the cell's text
  gr.Canvas.FillRect(Rect);
  gr.Canvas.TextOut(Rect.Left,Rect.Top,gr.Cells[ACol,ARow]);
end;

Hope that you enjoy...

Fulvio.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2538677
Hi slyna,

I would suggest to use a already made component (besides, why to reïnvent the wheel?). Here are some FW ones :

I've used the 3 first. Let me know if you want more info.

(tHyperGrid)
http://www.pablop.demon.co.uk/marley/thypergrid.htm
or
(tSuperGrid)
http://www.pablop.demon.co.uk/marley/tsupergrid.htm

(tAdvGrid)
http://www.tmssoftware.com/advgrid.htm

(tColorGrid)

http://www.csse.monash.edu.au/~vtran/

(tDataGrid)
http://www.ec-software.com/comppage.htm

etc..

Regards, Zif


0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2538688
Why I recommend the use of FW component :

Working with different fonts in a grid is not that easy to get a nice look. It will give you some headaches.

Of course if the code Fulvio is more then sufficient, don't hesitate to use it!

Regards, Zif.
0
Technology Partners: 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!

 

Author Comment

by:slyna
ID: 2538886
Wonderfull, just tell me how to check if a string is convertable to a float then you will get your points!

something like this:

if isvalidfloat(stringgrid2.cells[2,4]) then
 blaha
else
 blah
0
 

Author Comment

by:slyna
ID: 2538890
Mr fulvio_brasil

What the heck, you'll get your points anyway, but please answer the question above.

Thanks!
0
 
LVL 1

Expert Comment

by:fulvio_brasil
ID: 2539148
Hi slyna,

This code works very well, but, if you are in DELPHI IDE you will see a lot of debug's breaks about the except. Out of IDE, your app will work very well.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var gr: TStringGrid;
begin
  gr := (Sender as TStringGrid);

  try
    gr.Canvas.Font.Color := clBlack;
    StrToFloat(gr.Cells[ACol,ARow]);
  except
    gr.Canvas.Font.Color := clGray;
  end;

  if (ARow=gr.Row) AND (ACol=gr.Col)
    then gr.Canvas.Font.Color := clRed;

  gr.Canvas.FillRect(Rect);
  gr.Canvas.TextOut(Rect.Left,Rect.Top,gr.Cells[ACol,ARow]);
end;

If you are using Delphi 4 or more, go to menu <tools>, <debuger options>, <language exception> tab and add to ignore 'EConvertError' and, until in IDE you won't have problems.

Fulvio.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…
Suggested Courses

670 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