Search This Blog

gravatar

Delphi: Menampilkan gambar di sel DBGrid

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bitmap : TBitmap;
fixRect : TRect;
bmpWidth : integer;
imgIndex : integer;

Begin
   fixRect := Rect;

   // customizing the 'LastName' field
   if Column.Field = EmployeeTableLastName then
   begin
     //adding some logic to grab the required image
     if EmployeeTableSalary.Value > 50000 then
       imgIndex := 0
     else if EmployeeTableSalary.Value > 25000 then
       imgIndex := 1
     else
       imgIndex := 2;


     bitmap := TBitmap.Create;
     try
     //grab the image from the ImageList 
     //(using the "Salary" field's value)
       ImageList1.GetBitmap(imgIndex,bitmap);
     //Fix the bitmap dimensions
       bmpWidth := (Rect.Bottom - Rect.Top);
       fixRect.Right := Rect.Left + bmpWidth;
     //draw the bitmap
       DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
     finally
       bitmap.Free;
     end;

     // reset the output rectangle, 
     // add space for the graphics
     fixRect := Rect;
     fixRect.Left := fixRect.Left + bmpWidth;
   end;

   //draw default text (fixed position)
   DBGrid1.DefaultDrawColumnCell(fixRect, DataCol, Column, State);
end;