Solved

Colors in DBGrid

Posted on 2002-04-29
7
272 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
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
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.

 
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

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

Suggested Solutions

Title # Comments Views Activity
automatically logging in via bde delphi 3 68
Delphi TcxGrid group footer summary 3 203
Print Graphic and Text to Epson TM-T88v 12 169
DBGrid or StringGrid ? 6 63
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A short film showing how OnPage and Connectwise integration works.

919 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