inserting image in stringgrid cell

user1071339 picture user1071339 · Mar 29, 2012 · Viewed 14.1k times · Source

i m using stringgrid in my application.The data is fetched from the database(backend mysql) and displayed in stringgrid.

enter image description here

I want to insert image in status cell of each row. i.e.

      if status =online then -->image1
      else --->image2

anyone has any idea regarding how to do this?

Answer

LU RD picture LU RD · Mar 29, 2012

You will have to implement the OnDrawCell event.

Example :

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Longint;
  Rect: TRect; State: TGridDrawState);
var
  s: string;
  aCanvas: TCanvas;
begin
  if (ACol <> 1) or (ARow = 0) then
    Exit;
  s := (Sender as TStringGrid).Cells[ACol, ARow];

  // Draw ImageX.Picture.Bitmap in all Rows in Col 1
  aCanvas := (Sender as TStringGrid).Canvas;  // To avoid with statement
  // Clear current cell rect
  aCanvas.FillRect(Rect);
  // Draw the image in the cell
  if (s = 'online') then
    aCanvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Bitmap)
  else 
    aCanvas.Draw(Rect.Left, Rect.Top, Image2.Picture.Bitmap);
end;