Mega Code Archive

 
Categories / Delphi / ADO Database
 

Changing the font style of a dbgrid row

Title: Changing the font style of a dbgrid row Question: How can I change the font style of one particular row in a dbgrid ? Answer: Use the OnDrawDataCell event of the dbgrid. procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin // If the record's CustNo is 4711 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 = '4711' 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; Note: The code uses the DBDEMOS table "CUSTOMER.DB", a TDBGrid, a TDataSource and a TTable.