|
Technical answers from the trenches |
|
tDBGrid: Painting Graphics for Field Values
| ||||
Posted: 30 August 2000 |
||||
  |
Tested in Delphi 5 |
|||
  |
Audience: Beginner |
|||
Question: I'm using a DBGrid to display a Logical field. By default, the values appear as True or False. How can I display checkmarks instead?Answer: You can use the OnDrawColumnCell event to paint bitmaps in the column displaying the Logical field. Here's one implementation:
procedure TForm1.DBGrid1DrawColumnCell( Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState );
var
bmpImage : tBitmap; // temp. bitmap written to grid canvas
intX, intY : integer; // position of the bitmap in rectangle
begin
if column.fieldName = 'LogicalField' then
with DBGrid1.canvas do
begin
fillRect( rect ); // clear the value
bmpImage := tBitmap.create;
try
// choose appropriate bitmap
if Table1LogicalField.asBoolean then
bmpImage.assign( imgChecked.Picture.bitmap )
else
bmpImage.assign( imgUnCheck.Picture.bitmap );
// center the image (assuming it's smaller than rect)
intX := ( ( rect.right - rect.left ) div 2 ) -
( bmpImage.width div 2 );
intY := ( ( rect.bottom - rect.top ) div 2 ) -
( bmpImage.height div 2 );
draw( rect.left + intX, rect.top + intY, bmpImage );
finally
bmpImage.free; // only you can prevent resource leaks.
end; // try
end; // if/with
end;
Notes and Tips:
|
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|