Solved

Colors in DBGrid

Posted on 2002-04-29
7
278 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

623 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