Re: Delphi TDrawGrid elements

[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by webmaster Guido on June 18, 2003

In Reply to: TDrawGrid elements posted by Edouard Marmier, p12332 on June 17, 2003:

: I'm having a bad time with all these new classes and the documentation given or not given in the help system..
: E.g. the TStringGrid class has elements of type CELLS.
: I can't find out what the elements of TDrawGrid are.
: How do I color, for example, the element in row 2, column 3 of a Drawgrid in yellow?
: And how could I have found the answer to this by myself?
-----------------

Both the TStringGrid and the TDrawGrid are derived from TCustomGrid, but TDrawGrid has less properties than TStringGrid.
If you look at the properties of TDrawGrid in Delphi's Help, it says:

Derived from TCustomGrid:
  BorderStyle
  Col
  ColCount
  and so on...

The Help says this about the properties of TStringGrid:

In TStringGrid:
  Cells
  Cols
  Objects
  Rows
Derived from TCustomGrid:
  BorderStyle
  Col
  ColCount
  and so on...

Because a TDrawGrid has no Cells property, you have to do the drawing yourself in the OnDrawCell event handler.
Below is an example that shows the column and the row number, in these colors:
- black text a yellow background in the cell in column 2, row 3;
- white text on a navy background if the cell is selected;
- black text on a white background in the other cells;
- except: if the cell is a fixed cell, leave the default colors.

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: string;
begin
  S := 'C' + IntToStr(ACol) + ' , R' + IntToStr(ARow);
  with (Sender as TDrawGrid) do begin
    with Canvas do begin
      if (ARow = 2) and (ACol = 3) then begin
        Font.Color := clBlack;
        Brush.Color := clYellow;
        FillRect(Rect); // fill rectangle with yellow
      end
      else if (gdSelected in State) then begin
        Font.Color := clWhite;
        Brush.Color := clNAvy;
        FillRect(Rect); // fill rectangle with navy
      end;
      // Next, draw the text in the rectangle
      TextOut(Rect.Left + 2, Rect.Top + 2, S);
    end;
  end;
end;

A TStringGrid on the other hand, is drawn "automatically": the contents of each "Cell" is drawn in the color TStringGrid.Font.Color, on a background with color TStringGrid.Color; for example: black on white. So, if you just want to show text strings, it's easier to use a TStringGrid.

Also in a TStringGrid you can change the colors, e.g. display the cells with a yellow background, if they contain a text that starts with a "minus sign":

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: string;
begin
  with (Sender as TStringGrid) do begin
    S := Cells[ACol, ARow];
    if (Length(S) > 0) and (S[1] = '-') then begin
      with Canvas do begin
        Brush.Color := clYellow;
        FillRect(Rect); // fill rectangle with yellow
        // Next, draw the text in the rectangle
        TextOut(Rect.Left + 2, Rect.Top + 2, S);
      end;
    end;  
  end;
end;

Related Articles and Replies:


[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]