Solved

Colors in DBGrid

Posted on 2002-04-29
7
276 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
Industry Leaders: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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