Solved

Coloring rows in a DBGrid depending on values

Posted on 2001-08-27
14
289 Views
Last Modified: 2010-04-06
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.

Thanks in advance,
QUIQUE.
0
Comment
Question by:quique
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6428310
listening . . .
0
 
LVL 1

Author Comment

by:quique
ID: 6428333
Please it urges me, so listen as quick as possible ...

:-)

QUIQUE.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6428339
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
 
LVL 9

Expert Comment

by:ITugay
ID: 6428365
and litle bit about logic.

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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6428422
quicker listening . . . :-)
(have not the time for a workout)
0
 
LVL 1

Author Comment

by:quique
ID: 6428550
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.

If you need any questions, please ask me.

QUIQUE.
0
 
LVL 1

Author Comment

by:quique
ID: 6428553
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.

If you need any questions, please ask me.

QUIQUE.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 9

Expert Comment

by:ITugay
ID: 6428581
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
 
LVL 1

Author Comment

by:quique
ID: 6428762
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
 
LVL 2

Expert Comment

by:danysz
ID: 6430677
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
 
LVL 1

Author Comment

by:quique
ID: 6430957
No need to buy, thanks.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 300 total points
ID: 6431289
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
 
LVL 1

Author Comment

by:quique
ID: 6431335
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.

Thanks for your help,
QUIQUE.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6431347
Hi quique,

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

-----
Igor.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

14 Experts available now in Live!

Get 1:1 Help Now