Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dbgrid row fonts

Posted on 2002-07-15
6
Medium Priority
?
431 Views
Last Modified: 2013-12-02
I have a dbgrid on a form and I need to change the font colour of individual rows depending on a value in that row. Is there any way of doing this?
0
Comment
Question by:riccohb
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7154667
yes, Write an OnDrawColumnCell event handler to provide customized drawing for the data in the cells of the grid. Draw on the cell using the methods of the Canvas property.

set the grid default drawing to false
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7154686
here's a sample code. it draw buttons on a single column grid so it looks like just a column of buttons. I used OnDrawDataCell  even tho it's supposed to be obsolete...

the button bitmaps are held in image1 and image2, not exactly what you want but it should give you the idea...

if the text has a '^' in it, it makes smaller font and writes it on two lines. It was for an app where the form 'look and feel' was dependant on database table contents.

Anyway, you get the idea...

procedure TfrmOrder.grdGroupsDrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  Text, TextA, TextB : string;
  fSize : integer;
  TopOffset1, TopOffset2 : integer;
  LeftOffset : integer;
begin
  with (Sender as TwwDBGrid).canvas do
    begin
      if field = nil then exit;
      if Field.AsString = '' then
        begin
          Brush.Color := clBlack;
          FillRect(rect);
          exit;
        end;
      Text := Field.AsString;
      if Pos('^',Text) > 0 then
        begin
          TextA := Copy(Text,1,Pos('^',Text)-1);
          TextB := Copy(Text,Pos('^',Text) + 1, Length(Text));
          fSize := 12;
          TopOffset1 := 5;
          TopOffset2 := 11;
        end
      else
        begin
          TextA := Text;
          TextB := '';
          fSize := 18;
          TopOffset1 := 10;
          TopOffset2 := 20;
        end;
      if gdSelected in State then
          StretchBlt(Handle,
                     Rect.left,
                     Rect.top,
                     Rect.Right - Rect.Left,
                     Rect.Bottom - Rect.Top,
                     image1.canvas.handle,
                     image1.ClientRect.left,
                     image1.ClientRect.top,
                     image1.ClientRect.Right - image1.ClientRect.Left,
                     image1.ClientRect.Bottom - image1.ClientRect.Top,
                     SrcCopy)
      else
          StretchBlt(Handle,
                     Rect.left,
                     Rect.top,
                     Rect.Right - Rect.Left,
                     Rect.Bottom - Rect.Top,
                     image2.canvas.handle,
                     image2.ClientRect.left,
                     image2.ClientRect.top,
                     image2.ClientRect.Right - image2.ClientRect.Left,
                     image2.ClientRect.Bottom - image2.ClientRect.Top,
                     SrcCopy);
          font.color := clWhite;
          font.size := fSize;
          LeftOffset := (Rect.Right - Rect.Left - TextWidth(TextA)) div 2;
          TextOut(Rect.left + LeftOffset, Rect.top + TopOffset1, TextA);
          LeftOffset := (Rect.Right - Rect.Left - TextWidth(TextB)) div 2;
          TextOut(Rect.left + LeftOffset, Rect.top + TopOffset2 + fSize, TextB);
     end;
end;
0
 

Author Comment

by:riccohb
ID: 7154820
Another slight problem. I've added an OnDrawDataCell event, but it isn't being called. I've added a breakpoint on the first line of the procedure but it never stops there. (I've set DefaultDrawing to False).

Any ideas?
0
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!

 
LVL 1

Accepted Solution

by:
plaster earned 800 total points
ID: 7156914
May or may not but this works for me :)

procedure TDelivery.DBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var OldBrush,
    OldFont: TColor;
      Index: Integer;
begin
  with (Sender as TDBGrid), Canvas do begin
    OldBrush:=Brush.Color;
    OldFont:=Font.Color;

   try
    if {(Sender as TDBGrid).DataSource.DataSet.FieldByName('qtydel').AsString}= (Sender as TDBGrid).DataSource.DataSet.FieldByName('qty').AsString then begin
      if gdSelected in State then begin
        Brush.Color:=clNavy;
        Font.Color:=clwhite;
      end
      else begin
        Brush.Color:=clyellow;
        Font.Color:=clred;
      end
    end;
   except
     Brush.Color:=OldBrush;
     Font.Color:=OldFont;
   end;
   DefaultDrawColumnCell(Rect, DataCol, Column, State);
   Brush.Color:=OldBrush;
   Font.Color:=OldFont;
 end;
0
 
LVL 1

Expert Comment

by:plaster
ID: 7156918
remove {  }  from if statement
0
 

Author Comment

by:riccohb
ID: 7156987
Like a charm :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

604 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