Coloring rows in a DBGrid depending on values

Hello. I am having trouble in working with component TDBGrid in Delphi 4, as I don't know how to paint entire rows permanently.

I will explain my problem in detail:

I am doing an application that needs to control how much time people are inside a room. I have a form with a DBGrid with all the possible people stored in a Paradox table. Then I click a button and, in this precise moment, the record which is being selected in the grid, is supposed to get into the room. Then, from that moment that gets into the room (when I click some button) I have a timer controlling how much time that person spends inside the room. The timer checks every 5 minutes that the person has not overpassed some time limit. If so, I need the grid to paint in color RED, for example, the row corresponding to that person, and leave the rest of the rows as they are. I also would need to paint in different colors depending on time intervals I previously would define. The result would be a DBGrid with different permanent colors in some rows indicating how much time are the people in the room.

Can you understand what I need? It is a simple concept, but hard to program for me.

Ask anything if you have doubts.

If you think you have the solution, please copy & paste example code to make the solution easier.

QUIQUE.
LVL 1
Who is Participating?

Commented:
Hi quique,
ok, seems I understand it from the first comment.
Let some things more clear.

You have a table with persons.

Once person come to the some  place you start a timer to count amount of time that person spent in that place.

Every timer event you'd like to see how much time person is there.

Depending of time spent you need to paint row in according to time color.

All of this clear. But what happens if person is out? Do you need to reset time that person spent or just put it on hold while person will come next time?

-----
Igor.

PS: Let me know if you need to have about ready-to-use app. In this case I need to know "persons" table structure and map of colors depending of time in minutes.

0

Commented:
listening . . .
0

Author Commented:
Please it urges me, so listen as quick as possible ...

:-)

QUIQUE.
0

Commented:
Hi quique,
meikl :-)

>>Please it urges me, so listen as quick as possible ...
LOL

I'm not sure that it's what exactly you need, but may be something like this:

// empty class declaration to access some protected methods
type
TXDBGrid = class(TDBGrid)
end;

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
var
// the value of time spent in the room by given person
TimeSpent: Integer;
begin
with DBGrid1.Canvas do
begin
// prevent from changing bckground for focused cells
if not (gdFocused in State) then
begin
// extract time value from current record
TimeSpent := Query1.Fields[1].AsInteger div 20;
// build brush color depending of TimeSpent value
Brush.Color := RGB(255-TimeSpent, 0, 255);
end;

// call default drawing for the cell
TXDBGrid(DBGrid1).DefaultDrawDataCell(Rect, Field, State);
end;

end;
0

Commented:

you should have special field in "persons" table  to count amount of time person spent in the room.
this field is 0 by default.
once person enter the room you should set this field to 1, this mean that person in the room.
every timer interval you can execute SQL statement:

update persons set timespent = timespent + 1
where timespent > 0

this statement will increase by 1 field TimeSpent only for those person who is currently in the room.
also you need to close and then open "persons" table on your form to show last results.

-----
Igor.
0

Commented:
quicker listening . . . :-)
(have not the time for a workout)
0

Author Commented:
To ITugay:
==========
Ok. That's the way, but not exactly. The problem is that I have to execute an SQL statement in the OnTimer event from the Timer object. Dependening on the result of that SQL statement I need to colorize specific rows in the grid and those colors must be permanent, I mean, not dissapear when the row looses the focus.

QUIQUE.
0

Author Commented:
To ITugay:
==========
Ok. That's the way, but not exactly. The problem is that I have to execute an SQL statement in the OnTimer event from the Timer object. Dependening on the result of that SQL statement I need to colorize specific rows in the grid and those colors must be permanent, I mean, not dissapear when the row looses the focus. So what I need is to call some paint procedure from outside the grid in order to colorize specfic rows. See the problem? I can't do the processing inside the OnDrawDataCell event, but from outside.

QUIQUE.
0

Commented:
Hi quique,

yes, I have a question. Why don't you want to keep all necessary for painting parameters in database table?
Having data in table (let say calculated color for specific record) you will get permanent color.
It would be much easy to perform drawing from OnDrawDataCell event. Moreover, I don't think that it is possible to draw on DBGrid from outside of it.

----
Igor.
0

Author Commented:
Hi ITugay,

that's the main problem I find. I don't know the color until some time goes by. For example,

Imagine you come to my business. In order to know how much time you spend in my business I press a button to say to the system that you are in. Then a Timer object starts counting the time you spend here. But, as I need to control this time, every 5 minutes I check who, for example, has been inside for more than 20 minutes, or 30 or 1 hour. If someone is more than 20 but less than 30 then I need to paint that row where the person appears in the grid wiht green, for example. If the next case with yellow and if more than an hour, with red for example.

You understand what I am trying to do?

QUIQUE.
0

Commented:
Hello

If u need it so fast then go to Developer Express and buy the quantum grid or the master view ( check which is best for you) and u have everything u want. It's not a cheap but it's doing the work very good.

daniel
0

Author Commented:
0

Author Commented:
Hi ITugay,

as sometimes happens, I have found the solution to my problem through your comments. The problem was that I was querying from a table independent from the grid, so when I tried to paint the rows, as I could not make reference to fields in the external table linked to the grid itself, I couldn't do what I needed.

The solution has been to include all the query into the same query that links with the grid. And, of course, some little changes. But now it all works fine.

QUIQUE.
0

Commented:
Hi quique,

I'm glad that all OK for you.
You are wellcome :-)

-----
Igor.
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.