Re: writing an array into Delphi's DrawGrid

Posted by webmaster Guido
In Reply to: how writing an array into a drawgrid

: I would like to know how to write an array in a drawgrid.
: I have a file with much thousand of rows but I can't understand what code use because there isn't cell property in Drawgrid.
: I want to use drawgrid because it's fast than stringgrid according a code published in this forum.
-------------------------------------

Delphi's DrawGrid is a simplified version of a StringGrid. The StringGrid allows you to display text strings by simply storing the strings in the property Cells[ColumnNr,RowNr] of the grid. On the other hand, the DrawGrid requires you to draw whatever you want displayed.

Like most components, a DrawGrid has a "Canvas" property, the surface on which you draw. On the Canvas, you can draw text as well as images. You have to put the code for drawing in the event handler of the DrawGrid, as in the example below.

Example: display the row numbers in the first column, and display the word "DrawGrid" in the second column of every row:

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with DrawGrid1 do begin
    if ACol = 0 then
      Canvas.TextOut(Rect.Left, Rect.Top, IntToStr(ARow))
    else
      Canvas.TextOut(Rect.Left, Rect.Top, 'DrawGrid');
  end;
end;

Example 2: display the contents of the two-dimensional array MyStrings in a Delphi DrawGrid. The MyStrings array contains a string in every element, and of course its dimensions must fit the DrawGrid (number of rows and columns).

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  DrawGrid1.Canvas.TextOut(Rect.Left, Rect.Top,  MyStrings[ACol, ARow]);
end;

Find related articles

Search for:  Array  Drawgrid  


Delphi Forum :: Tutorials :: Source code :: Tips