Posted by webmaster Guido
: I would like to know how to write an array in a drawgrid.
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;