Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

Custom Draw in NextGrid

Related Articles:

Info
  • Skill:
  • Version: 1.0.1
  • Author: Bojan Nikolic
  • Created:
  • Updated: 2008-05-13

1. Set column for custom draw



Cell is not drawn in custom mode by default. This is controled by DrawingOptions property of Column:

Table 1: DrawingOptions
Name Meaning
doNormal Cells within this column are drawn without custom draw.
doCustom Cell will be drawn as usuall, but custom code may be added as extra painting layer over existin drawing.
doCustomOnly Cell must be drawn manualy, even a background
doBackgroundOnly Background is drawn as first layer (may be selected or not), but additional drawing must be done manualy.


2. Custom Draw Event



If you like to made some custom drawing inside cell, you will need to use OnCustomDrawCell event. This event occur, when every Cell inside Grid is drawn.

This event have next parameters:

Table 2: Parameters
Name Meaning
ACol Column Index of currently drawing cell
ARow Row Index of currently drawing cell
CellRect TRect of currently drawing cell
CellState Cell state of currently drawing cell


ACol and ARow parameters are useful for checking cell content of this cell before you draw.

Delphi Example:

if NextGrid1.Cells[ACol, ARow] = 'completed' then


if(NextGrid1.Cells[ACol][ARow] = "completed" {


CellRect help when drawing need to fit inside currently drawing cell.

NextGrid1.Canvas.MoveTo(CellRect.Left, CellRect.Top);
NextGrid1.Canvas.LineTo(CellRect.Right, CellRect.Bottom);


NextGrid1->Canvas->MoveTo(CellRect->Left, CellRect->Top);
NextGrid1->Canvas->LineTo(CellRect->Right, CellRect->Bottom);


This example draw cross line inside current cell.

CellState parameter identify state (if cell selected or not) of currently painted cell.

This parameter is useful for different drawing for selected and non-selected cells.

Full example:

procedure TForm1.NextGrid1CustomDrawCell(Sender: TObject; ACol,
  ARow: Integer; CellRect: TRect; CellState: TCellState);
var
  R: TRect;
begin
  R := CellRect;
  InflateRect(R, -2, -2);
  with NextGrid1.Canvas do
  begin
    if csSelected in CellState then
      Pen.Color := clHighlightText
    else
      Pen.Color := clGray;
    Brush.Color := clRed;
    Rectangle(R);
  end;
end;


Result:



3. Background Custom Draw



Background may be custom drawn too, very similar as drawing custom cells.

This need to be done within OnCustomDrawCellBackground event.

When DefaultDrawing parameter is set to False standard drawing will be skipped, and custom drawing will be applied.

Table 3: Parameters
Name Meaning
ACol Column Index of currently drawing cell
ARow Row Index of currently drawing cell
CellRect TRect of currently drawing cell
CellState Cell state of currently drawing cell
DefaultDrawing When set to False, custom drawing will be used.


Example of drawing "3D" effect cells:

procedure TForm1.NextGrid1DrawCellBackground(Sender: TObject; ACol,
  ARow: Integer; CellRect: TRect; CellState: TCellState;
  var DefaultDrawing: Boolean);
var
  R: TRect;
begin
  if not(csSelected in CellState) then
  begin
    DefaultDrawing := False;
    with NextGrid1.Canvas do
    begin
      R := CellRect;
      Frame3D(NextGrid1.Canvas, R, clBtnHighlight, clBtnShadow, 1);
      Brush.Color := clBtnFace;
      FillRect(R);
    end;
  end;
end;


Result:

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.