Solved

dbgrid row fonts

Posted on 2002-07-15
6
421 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
  • 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
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

Accepted Solution

by:
plaster earned 200 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

23 Experts available now in Live!

Get 1:1 Help Now