3

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

在此处输入图像描述

我想在每一行的状态单元格中插入图像。IE

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

有人对如何做到这一点有任何想法吗?

4

1 回答 1

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;
于 2012-03-29T11:41:20.813 回答