Search This Blog

Archives

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;

gravatar

Delphi : Scrolling DBGrid

Memfungsikan Mouse Scroll pada GRID
- Buat Procedure berikut :

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.message = META_SETTEXTJUSTIFICATION) then
    if ActiveControl is TDBGrid then
    begin
      if msg.wParam >0 then
        SendMessage(ActiveControl.Handle,WM_KEYDOWN,VK_UP,0)
      else
        SendMessage(ActiveControl.Handle,WM_KEYDOWN,VK_DOWN,0);
   handled := true;
   end
end;


- Tambahkan pada Main Form event Form Create :
  Application.OnMessage := MyMessageHandler;