Solved

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

Posted on 2002-06-24
23
229 Views
Last Modified: 2012-05-04
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
Comment
Question by:ivylnm
  • 7
  • 5
  • 2
  • +6
23 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7102956
is it a dbgrid or a stringgrid?
0
 

Author Comment

by:ivylnm
ID: 7102997
String Grid.  
0
 

Author Comment

by:ivylnm
ID: 7103039
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
 
LVL 1

Expert Comment

by:drnadeem
ID: 7103270
listening
0
 
LVL 6

Assisted Solution

by:DrDelphi
DrDelphi earned 60 total points
ID: 7105183
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
 
LVL 12

Assisted Solution

by:Lee_Nover
Lee_Nover earned 60 total points
ID: 7105197
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
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7105203
hehe DrDelphi answered faster ... :)
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7105340
<g>
0
 

Author Comment

by:ivylnm
ID: 7106257
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
 

Author Comment

by:ivylnm
ID: 7106381
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7106477
additional you must set the defaultdrawing-property to false

meikl ;-)
0
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.

 

Expert Comment

by:jamiguel
ID: 7106504
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7106524
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
 

Author Comment

by:ivylnm
ID: 7106615
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
 

Author Comment

by:ivylnm
ID: 7106622
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 80 total points
ID: 7106624
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
 

Author Comment

by:ivylnm
ID: 7106634
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
 
LVL 3

Expert Comment

by:SteveWaite
ID: 7120815
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
 

Expert Comment

by:jamiguel
ID: 7120999
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
 

Expert Comment

by:CleanupPing
ID: 9343169
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
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9453536
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9453558
just to correct:
instead of drnadeem, drDelphi should get the split
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 9455251
my idea to study the calendar component was sound advice too :-)
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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

15 Experts available now in Live!

Get 1:1 Help Now