• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

How to color a cell in any other Grid except DBGrid ?

Assume that there is a grid like this :-

Code  Name  Age   Salary   Overtime
----  ----  ---   ------   --------
A1                XXX       YYY

When I  type in A1 as Code, I will perform a checking whether to allow this person to have any salary entry.  If he is not allowed to have any salary entry, the cell "XXX" should be colored as GRAY and it should be blank.  And another checking on whether this person is entitled for OVertime, if not entitled, then it wil be filled up with RED color.

Please guide.
0
ivylnm
Asked:
ivylnm
  • 7
  • 5
  • 2
  • +6
3 Solutions
 
kretzschmarCommented:
is it a dbgrid or a stringgrid?
0
 
ivylnmAuthor Commented:
String Grid.  
0
 
ivylnmAuthor Commented:
I am not sure on how to use the OnDrawCell procedure.  Does that help in solving my problem ??  I have seen most examples of coloring but they are mostly useful only when data are loaded up for display.  In my case, I need the grid to response to the data that I have just entered such as I enter my name and from the database, I am not allowed to have Overtime claim, then I want the cell where OverTime amount is entered being colored to gray and not able for user to key in anything.  Please help.  I need this solution in shorter time.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
drnadeemCommented:
listening
0
 
DrDelphiCommented:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   IF ACOL=0 THEN
   BEGIN
      if StringGrid1.Cells[1,aRow]='XXX' then
      begin
        StringGrid1.Canvas.Brush.Color:=clGray;
        StringGrid1.Canvas.Pen.Color:=clWhite;
        StringGrid1.Canvas.FillRect(StringGrid1.CellRect(1,ARow));
      end;
        ///etc, etc, etc....
   END;


end;


Good luck!!
0
 
Lee_NoverCommented:
here's a simple custom draw example

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
     if State = [] then
     begin
       if StringGrid1.Cells[ACol, ARow] = 'lala' then
       begin
         StringGrid1.Canvas.Font.Color:=clBlue;
         StringGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, StringGrid1.Cells[ACol, ARow]);
       end;
     end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     StringGrid1.Cells[2, 2]:='lala';
end;

it just colors the cell text containing 'lala' with blue
for anything else you simply draw on the canvas like in the example
you do whatever checking you need be done
0
 
Lee_NoverCommented:
hehe DrDelphi answered faster ... :)
0
 
DrDelphiCommented:
<g>
0
 
ivylnmAuthor Commented:
I have a problem here.  I have tried to use DrDelphi's and Lee's codes.  And i have put a break point inside the DrawCell procedure.  But it does not really seem to go into that procedure.  What should i do ?
0
 
ivylnmAuthor Commented:
Does this DrawCell procedure response to the user entering some words inside the grid cell during run time ? Normally I can find examples on coloring on row or column where it happens only when retrieving data from database.
0
 
kretzschmarCommented:
additional you must set the defaultdrawing-property to false

meikl ;-)
0
 
jamiguelCommented:
see:

http://delphi.about.com/library/weekly/aa031699.htm

or if u  use stringgrids http://www.hoerstemeier.com/

download aligrid is good component

thanks...
0
 
kretzschmarCommented:
jamiguel,

its just not polite to answer a question,
when others already has given a solution
in previous comments

for the future, please,
do rather comment than answer a question,
so that the questioner can decide,
which comment helps best

meikl ;-)

0
 
ivylnmAuthor Commented:
But meikl,  when I set the default drawing to false, the title become all blank.  Besides, when I type something into Cells[x,y], and then I tab away into Cells[x+1, y] , the cells[x,y] become blank again.  But when my cursor moves into Cells[x,y], the data I type in become visible again.  What could have gone wrong here ?
0
 
ivylnmAuthor Commented:
I have visited the URL you have kindly provided,jamiguel. But I found out that those codes are useful when a grid is attached to a dataset and good for display.  The problem with me now is quite dynamic which depends on what is going to be keyed into the grid.  Please refer to the whole discussion for more details.  Thanks a lot ~
0
 
kretzschmarCommented:
well,
you must now draw each cell byself,
but this should not be hard,
because, if i have it correct in mind,
there is a defaultdrawcell-method (or similar named),
so you could easily expand drdelphis code like

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
 Rect: TRect; State: TGridDrawState);
begin
  IF ACOL=0 THEN
  BEGIN
     if StringGrid1.Cells[1,aRow]='XXX' then
     begin
       StringGrid1.Canvas.Brush.Color:=clGray;
       StringGrid1.Canvas.Pen.Color:=clWhite;
       StringGrid1.Canvas.FillRect(StringGrid1.CellRect(1,ARow));
     end;
       ///etc, etc, etc....
    ....
    else defaultdrawcell(...);
  END;

meikl ;-)
0
 
ivylnmAuthor Commented:
I have tried that before but I am not sure is there any setting that i have gone wrong.  When I tried to run the program, i found out that it does not even go into that procedure DrawCell.  Maybe if anyone got a small piece of codes, where a grid to let user to key in, and based on the input the cell will turn into different colors, Please send them to me at ivy@obmsoftware.com.

0
 
SteveWaiteCommented:
create your own component and override the DrawCell procedure
have a look at the source for similar delphi components
i think the calendar might be a good example

Regards
Steve
0
 
jamiguelCommented:
ok, i think that no has a problem i have same problem for you and i answer my question with theses links, i not understan why u are boothering, also see:

Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.

Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.

Comment Vs. Answer
If you are unsure of your solution, post it as a comment. Members can accept comments as solutions and award you Expert Points for them


i am sure for the answer, :)) :p


regards

sorry for my bad english
0
 
CleanupPingCommented:
ivylnm:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
Lukasz LachCommented:
ivylnm,
No comment has been added lately (17 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: split points between drnadeem http:#7103270 and Lee_Nover http:#7105197 and kretzschmar http:#7106624

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0
 
kretzschmarCommented:
just to correct:
instead of drnadeem, drDelphi should get the split
0
 
SteveWaiteCommented:
my idea to study the calendar component was sound advice too :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 5
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now