inserting image in stringgrid cell

14,585

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;
Share:
14,585
Admin
Author by

Admin

Updated on August 29, 2022

Comments

  • Admin
    Admin over 1 year

    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?

  • Admin
    Admin about 12 years
    +1 but I find using with as a bad example, but that doesn't mean that the OP can't modify it (:
  • Warren  P
    Warren P about 12 years
    Change with to aCanvas := (Sender as TStringGrid).Canvas and write aCanvas instead of Canvas and get rid of the with.
  • LU RD
    LU RD about 12 years
    @DorinDuminica and Warren, you are right. This is the only case I ever use the with statement, because I'm to lazy to define a helper var. Otherwise it should be banned for many reasons. Thanks.