在stringgrid单元格中插入图像

3 delphi image tstringgrid

我在我的应用程序中使用stringgrid.数据从数据库(后端mysql)获取并显示在stringgrid中.

在此输入图像描述

我想在每行的状态单元格中插入图像.即

      if status =online then -->image1
      else --->image2
Run Code Online (Sandbox Code Playgroud)

有谁知道如何做到这一点?

LU *_* RD 8

您必须实现OnDrawCell事件.

示例:

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;
Run Code Online (Sandbox Code Playgroud)