Solved

Colors in DBGrid

Posted on 2002-04-29
7
271 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
Comment Utility
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
Comment Utility
exactly
0
 
LVL 4

Accepted Solution

by:
nestorua earned 150 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
...
0
 
LVL 4

Author Comment

by:spk2000ar
Comment Utility
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
Comment Utility
HI,
It's much more reasonable to use TDBImage or TDBCtrlGrid components for your purpose.
Sincerely,
Nestorua.
0
 
LVL 4

Author Comment

by:spk2000ar
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

12 Experts available now in Live!

Get 1:1 Help Now