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
Solved

Coloring rows in a DBGrid depending on values

Posted on 2001-08-27
14
293 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
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.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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