inserting image in stringgrid cell

2019-02-27 05:34发布

问题:

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

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?

回答1:

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;