Solved

Colors in DBGrid

Posted on 2002-04-29
7
277 Views
Last Modified: 2010-04-04
Welcome you all!
What i need is this:
* Color the rows of a DBGrid which datasource is a Paradox table depending on the value of one of the fields of each record. eg: if field A gets the values 0, 1, 2 and 1 in four record then the first row must be red, the second blue, the third green and the last one blue again.
I am working with Delphi 6 Enterprise.
Thanks...
0
Comment
Question by:spk2000ar
[X]
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
7 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6979221
write a handler for the DBGrid's OnDrawColumnCell, e.g.:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  AColor: TColor;
begin
  case Table1.Fields[2].AsInteger of
    0: AColor := clRed;
    1: AColor := clBlue;
    2: AColor := clGreen;
  end;

  with Sender as TDBGrid do
  begin
    Canvas.Brush.Color := AColor;
    Canvas.FillRect(Rect);
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
end;



HTH
DragonSlayer
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6979826
exactly
0
 
LVL 4

Accepted Solution

by:
nestorua earned 150 total points
ID: 6980704
HI,
First of all, don't forget to make DefaultDrawing property
True.
In OnDrawColumnCell event write the following:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var OldBrush, OldFont: TColor;
begin
 with Sender as TDBGrid do
  with Canvas do
   begin
     OldBrush:=Brush.Color;
      OldFont:=Font.Color;
//
   if (DataSource.DataSet[YourFieldName]=0)
    then
     begin
      if gdSelected in State
       then
        begin
         Brush.Color:=clRed;
          Font.Color:=clAqua;
        end
       else
        begin
         Brush.Color:=$00DDDFFF;
          Font.Color:=clBlack;
        end;
       end;
//
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
//
     Brush.Color:=OldBrush;
      Font.Color:=OldFont;
end;
I don't write exactly what condition you use to highlight
your DBGrid with different colors.
Sincerely,
Nestorua.  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6981375
...
0
 
LVL 4

Author Comment

by:spk2000ar
ID: 6981745
Sorry about the delay but i check this only twice a day.
I try Nestorua solution (which is more accurate) and works fine for what i want.
The two solution get me into the idea.
One more, I now trying to get a graphic in the same grid in one column, one for each value of the same field.
I put it into the data of the table but I only get a (GRAPHIC) description. The graphic is a simple bitmap and i think i can wrote into the same code something like this

var bmp: TBitmap;
...

if (DataSource.DataSet[YourFieldName]=0)
   then
    begin
     if gdSelected in State
      then
       begin
        Brush.Color:=clRed;
        Font.Color :=clAqua;
        bmp := TBitmap.Create;
        imagelist1.GetBitmap(DataSource.DataSet[YourFieldName].AsInteger, bmp);
          Canvas.Draw(Rect.Top + 2, Rect.Left + 2,bmp);
          bmp.Free;
          DefaultDrawColumnCell(Rect, Field, State);
       end
...

But the bitmap is drawn at the top of the grid or behind it.
Thanks you
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6981809
HI,
It's much more reasonable to use TDBImage or TDBCtrlGrid components for your purpose.
Sincerely,
Nestorua.
0
 
LVL 4

Author Comment

by:spk2000ar
ID: 6986891
May be, but is faster with the DBGrid, because is just three types of square what i need to show. Besides, i do not get into the data that graphic.
If what i want has an easy solution i would like that, but the DBCtrlGrid may be the best solution.
Thanks, i wait for some answer to this and give you the answer...
Thanks you all!!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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