Coloring dbgrid rows when it meets one or more condition

Posted on 2011-04-26
Last Modified: 2012-05-11
Related to my previous question.

Is there a way to color certain coloumns a different color depending on the value of the field?

The first column is Doc_Type and I need to have it colored in 3 different colors depending the value eg: If value is Fax then Row is Red if value is Original = Green and if value is Copy = Yellow.
Question by:DigitalNam
    LVL 24

    Accepted Solution

    You are going to use the Canvas properties at the DBGridDrawColumnCell event of the DBGrid.
    You may check this article which is going to answer many of your questions.
    LVL 1

    Author Closing Comment

    Thanks JimyX. At the rate I am going today you are going to earn ALOT of points. Hehe
    LVL 24

    Expert Comment

    Could you try this please:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
        DataCol: Integer; Column: TColumn; State: TGridDrawState);
      DBGrid1.Canvas.Brush.Color := clWhite;  // the default color
      if DB.FieldByName('Doc_Type').AsString = 'Fax' then //you can use uppercase() to handle case sensitivity, if required
        DBGrid1.Canvas.Brush.Color := clRed;
      if DB.FieldByName('Doc_Type').AsString = 'Original' then
        DBGrid1.Canvas.Brush.Color := clGreen;
      if DB.FieldByName('Doc_Type').AsString = 'Copy' then
        DBGrid1.Canvas.Brush.Color := clYellow;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);

    Open in new window

    LVL 24

    Expert Comment

    Am ready and cracking my fingers waiting  :)
    LVL 1

    Author Comment

    Get ready for the next question.. :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    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

    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…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 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