Solved

DBGrid & row color

Posted on 2004-03-24
8
790 Views
Last Modified: 2010-04-05
I have a field with nexts possible values : 'o' , '' or 'n'. I wish that in my DBGrid to show with green rows all lines what have 'o' value assigned, red for 'n' and white for ''.

Attention, I wish all ROW colored and NOT only CELL where is displayed 'o' or 'n'.
0
Comment
Question by:ginsonic
[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
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10669128
 begin
    { Set default font color }
    DBGrid1.Canvas.Font.Color := clBlack;
    { Is current cell the focused cell? }
    if (gdFocused in State) then begin
      DBGrid1.Canvas.Brush.Color := clBlack;
      DBGrid1.Canvas.Font.Color := clWhite;
    end
    { Not focused, is it the target field Terms? }
    else if (Field.FieldName = MYFIELDNAMEHERE) then
      { Check field content for one of two possible values }
      if (Field.AsString = 'o') then
        { If field contains "o" paint  green}
        DBGrid1.Canvas.Brush.Color := clGreen
      else
        { If field contains "n" paint red }
        DBGrid1.Canvas.Brush.Color := clPaleRed
    else
      { Paint non-focused and non-target cells white }
      DBGrid1.Canvas.Brush.Color := clWhite;
       DBGrid1.DefaultDrawDataCell(Rect, Field, State);
  end;

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10669143
Oops, the code should be place in the TDBGrids OnDrawDataCell event

see the entir article here:

http://community.borland.com/article/0,1410,20845,00.html

Shane
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 250 total points
ID: 10669202
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
if TDBGrid(Sender).DataSource.DataSet.FieldByName('ThisField').AsString ='n'  then begin
    DBGrid1.Canvas.Brush.Color := clRed;
    DBGrid1.Canvas.Font.Color := clYellow;
    end  else
    if TDBGrid(Sender).DataSource.DataSet.FieldByName('ThisField').AsString ='o' then begin
    DBGrid1.Canvas.Brush.Color := clgreen;
    DBGrid1.Canvas.Font.Color := clWhite;
    end else
    if TDBGrid(Sender).DataSource.DataSet.FieldByName('ThisField').AsString ='' then begin
    DBGrid1.Canvas.Brush.Color := clwhite;
    DBGrid1.Canvas.Font.Color := clblack;
    end;
  DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;
0
Technology Partners: 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 9

Author Comment

by:ginsonic
ID: 10669375
Attention, I wish all ROW colored and NOT only CELL where is displayed 'o' or 'n'.

Your code change the color just for cell. This can be done more easier in DrawColumn.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 10669385
The message was for shaneholmes :)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10669472
sorry, i extrapolated from the link i provided, and didn't change everything that was appropriate to your question

it could have easily been changed to test the fieldbyname versus each individual field

if (DBGrid1.FieldByName = MYFIELDNAMEHERE) = '0' then
        DBGrid1.Canvas.Brush.Color := clGreen
      else
if (DBGrid1.FieldByName = MYFIELDNAMEHERE) = '1' then
          DBGrid1.Canvas.Brush.Color := clPaleRed


Oh well, you win some, you loose some

Shane
0
 
LVL 9

Author Comment

by:ginsonic
ID: 10669732
Still color just a cell.

I have a new problem. In my project I have a DBGrid with specified columns. When I test these comments I used a demo project where I show all fields. In this case all work perfect.

But when I ported the demo code to my app don't work. Look alike DrawCell isn't called when I declare some columns. Some help here? New 250 points available.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 10669787
Found already a way in DrawColumn event. Thanks for support!
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 152
how to send memory stream from ics Client To ics server ? 11 204
When i run adoquery my application freezes 26 209
Firemonkey DbLookupComboBox equivalent ? 2 77
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

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