Главная страница | назад





Article #15978: Changing the font style of a dbgrid row

 Question and Answer Database

FAQ978D.txt   Changing the font style of a dbgrid row
Category   :Database/VCL
Platform    :All
Product    :All 32 bit  

Question:
How can I change the font style of one particular row in a 
dbgrid (one record)?


Answer:
Paste the code below into the OnDrawDataCell event of the dbgrid. 

Note: The code below uses the DBDEMOS table "CUSTOMER.DB", a TDBGrid, a TDataSource
and a TTable.

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
// If the record's CustNo is 1351 draw the entire row with a
// line through it. (set the font style to strike out)
  if (Sender as TDBGrid).DataSource.DataSet.FieldByName('CustNo').AsString = '1351' then
     with (Sender as TDBGrid).Canvas do begin
      FillRect(Rect);
      // Set the font style to StrikeOut
      Font.Style := Font.Style + [fsStrikeOut];
      // Draw the cell right aligned for floats + offset
      if (Field.DataType = ftFloat) then
        TextOut(Rect.Right-TextWidth(Field.AsString)-3, 
        Rect.Top+3, Field.AsString)
      // Otherwise draw the cell left aligned + offset
      else
        TextOut(Rect.Left+2,Rect.Top+3,Field.AsString);
     end;
end;

7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99